using System.Collections; using System.Collections.Generic; using UnityEngine; public class AIChace : MonoBehaviour { //vareables public GameObject Player; public string triggeringItemTag = "YourItemTag"; private float distance; private Vector2 direction; public int range = 7; public float speed; //seting vareables to correct components private Rigidbody2D body; /// /// This is to get the riged body of the player at the start of the run /// void Start() { body = GetComponent(); } /// /// Update is called once per frame and calculates where the enemy is faceing, and the speed that /// the enemy is moving at /// void Update() { //calculating the distance between the player and the enemy distance = Vector2.Distance(transform.position, Player.transform.position); //calculating the diraction that the player should face direction = Player.transform.position - transform.position; direction.Normalize(); float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg; //checking if the player is within range and if so then moving towards the player if (distance < range) { body.velocity = direction * speed; transform.rotation = Quaternion.Euler(Vector3.forward * angle); } } /// /// is function first is run every time somthing colides with the enemy, and if it's taged bullet /// then distroys it self /// /// private void OnCollisionEnter2D(Collision2D collision) { //checking if the colition is with the object taged bullet, if so then distroying self if (collision.gameObject.CompareTag("Bullet")) { Destroy(gameObject); } } }