• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kamalaswal40 · Feb 23, 2021 at 02:23 PM · ai

Why my enemy not going on y-axis its only going in x-axis

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class enmey : MonoBehaviour { public float speed; public float stopping; public float retreat;

 public float startShot;
 public float reloadShot;

 public GameObject bullet;
 public GameObject bulletFollow;
 public Transform player;
 // Use this for initialization
 void Start () {
 
     reloadShot = startShot;
 }
 
 // Update is called once per frame
 void Update () {
     EnemyShot();
     EnemyMove();
    
 }

 void EnemyMove()
 {
     if(Vector2.Distance(transform.position, player.transform.position) > stopping)
     {
         transform.position = Vector2.MoveTowards(transform.position, player.transform.position, speed * Time.deltaTime);
     }
     else if (Vector2.Distance(transform.position, player.transform.position) < stopping && Vector2.Distance(transform.position, player.transform.position) > retreat)
     {
         transform.position = this.transform.position;
     }
     else if (Vector2.Distance(transform.position, player.transform.position) < retreat)
     {
         transform.position = Vector2.MoveTowards(transform.position, player.transform.position, -speed * Time.deltaTime);
     }
 }

 void EnemyShot()
 {
     if (reloadShot <= 0)
     {
         //no rotate
         var number = Random.Range(200, 200);
         
         if (number == 1)
         {
             Instantiate(bullet, player.transform.position, Quaternion.identity);
         }
         else
         {
             Instantiate(bulletFollow, player.transform.position, Quaternion.identity);
             
         }
         reloadShot = startShot;
     }
     else
     {
         reloadShot -= Time.deltaTime;
     }

 }
 private void OnCollisionEnter(Collision Collision) {
  Destroy(Collision.gameObject);
 //  if(Collision.collider)
   //{
     //   DestroyCollision.bulletFollow;
   //}
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by GeroNL · Feb 23, 2021 at 03:08 PM

Maybe you can not use minus speed. try to use this :

 else if (Vector2.Distance(transform.position, player.transform.position) < retreat)
 {
     Vector2 pos = transform.position;
     Vector2 dir = (transform.position - player.transform.position).normalized;
     // you need tweak in this, like multple the dir like
     // dir *= 2f;
     transform.position = Vector2.MoveTowards(pos , dir,  speed * Time.deltaTime); 
 }

Hope it help.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kamalaswal40 · Feb 23, 2021 at 03:28 PM 0
Share

Thanks for replaying but the problem isn't solve

avatar image GeroNL kamalaswal40 · Feb 23, 2021 at 04:00 PM 1
Share

ah sorry, you need to change this:

 transform.position = Vector2.MoveTowards(pos , pos+dir,  speed * Time.deltaTime); 


if it can't, may you need to use this : https://docs.unity3d.com/ScriptReference/Debug.DrawLine.html


try it after initial dir, may it can help you know the direction. (its use vector3, just set it xy or xz, left the not use 0).

avatar image kamalaswal40 GeroNL · Feb 24, 2021 at 04:07 AM 0
Share

thank you so much it's working.

but i need you help a bit more when my enemy shoot player. The bullet doesn't distory . please help me to distory bullet after 3 sec. please.....

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

191 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

AI script problems, interaction with grenade not working, need help. 2 Answers

Ai Zombie Melee Attack script. 5 Answers

Enemy following Player on uneven surface 1 Answer

About a enemy AI in my game 1 Answer

WayPoints mixed with Raycast 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges