- Home /
 
get enemy to chase and shoot at player wherever hes facing.
i tried this but when the player gets close to enemy the enemy starts flipping back n forth super quick
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 
 
 public class MoveShootRBDesktop : MonoBehaviour
 {
 
     public Transform player;
     public float speed;
 
     private void Start()
     {
 
         //This finds the GameObject that has the PlayerController script attached.
         //This is assuming there is only one PlayerController.
         player = GameObject.FindGameObjectWithTag("Player").transform;
     }
 
     void Update()
     {
         float step = speed * Time.deltaTime;
         transform.position = Vector3.MoveTowards(transform.position, player.position, step);
 
         #region \/-----Face Right-----\/
         if (transform.position.x < player.position.x)
         {
             GetComponent<SpriteRenderer>().flipX = true;
         }
         #endregion
         #region \/-----Face Left-----\/
         else if (transform.position.x > player.position.x)
         {
             GetComponent<SpriteRenderer>().flipX = false;
         }
         #endregion
     }
 }
 
               i also tried this... but when i do this and i move the player down and the enemy follows a lil bit when he goes to attack he go back to starting position. There was something else i tried but cant find the code... i like the 2nd one i showed you but like i say he is glitchy! any help would be appreciated
     public float speed;
     private Transform player;
     public float lineOfSite;
     public float shootingRange;
     public GameObject Bullet;
     public Transform raycastPoint;
     public float lastAttackTime;
     public float attackDelay;
     public float shootingForce;
     private SpriteRenderer spriteRenderer;
     private bool isMoving;
     private bool facingDown;
     private Rigidbody2D rb;
     private Vector2 move;
 
 
 
 
     void Start()
     {
         player = GameObject.FindGameObjectWithTag("Player").transform;
         this.spriteRenderer = this.GetComponent<SpriteRenderer>();
         rb = GetComponent<Rigidbody2D>();
         isMoving = false;
 
 
     }
     void Update()
     {
 
         this.spriteRenderer.flipX = player.transform.position.x < this.transform.position.x;
         float distanceFromPlayer = Vector2.Distance(player.position, transform.position);
         if (distanceFromPlayer < lineOfSite && distanceFromPlayer > shootingRange && isMoving)
         {
             transform.position = Vector2.MoveTowards(this.transform.position, player.position, speed * Time.deltaTime);
             isMoving = true;
         }
         else if (distanceFromPlayer <= shootingRange && !isMoving)
         {
             isMoving = false;
             if (Time.time > lastAttackTime + attackDelay)
             {
 
                 transform.position = (player.transform.position - this.transform.position).normalized;
                 GameObject effect = Instantiate(Bullet, raycastPoint.position, raycastPoint.rotation);
                 effect.GetComponent<Rigidbody2D>().AddRelativeForce(transform.position * shootingForce, ForceMode2D.Impulse);
                 lastAttackTime = Time.time;
 
 
 
 
             }
 
         }
     }
 
     private void OnDrawGizmos()
     {
         Gizmos.color = Color.green;
         Gizmos.DrawWireSphere(transform.position, lineOfSite);
         Gizmos.DrawWireSphere(transform.position, shootingRange);
 }   }
      with this one he does shoot where player is but he is kind of glitchy
 
              
               Comment
              
 
               
              Your answer