#need help with my enemy movement

1 messages · Page 1 of 1 (latest)

finite forge
#

i've implemented a simple movement script on my enemy objects, on my game about planes the enemy will go toward the current position of the player, stop at a certain distance and fall back if the player gets too close

public float speed;
   public Transform targetPoint;
   public int stopDistance = 3;
   public int fallBackDistance = 2;
   public int enemyDeck = 1;
  
   // get refrence to the player object
   void Start()
   {
       targetPoint = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();

   }
   private void Update()
   {
       if(Vector3.Distance(transform.position,targetPoint.position) > stopDistance)
       {
           transform.position = Vector3.MoveTowards(transform.position, targetPoint.position, speed * Time.deltaTime);
       }
       else if(Vector3.Distance(transform.position, targetPoint.position) < stopDistance && Vector3.Distance(transform.position, targetPoint.position) > fallBackDistance)
       {
             //make the enemy stop
             transform.position = this.transform.position
       }
       else if(Vector3.Distance(transform.position, targetPoint.position) < fallBackDistance) 
       {
           //make the enemy fall back
             transform.position =           Vector3.MoveTowards(transform.position, targetPoint.position, -speed * Time.deltaTime);
       }
       else if (transform.position.y < enemyDeck)
       {
           transform.position = this.transform.position; //stop enemy from going too far down
       }
   }

what i want to do is that i want the enemy to stop moving along the X axis but keep following the player on the y axis how can i do that. thanks,