Hi, Im making my first project in Unity and right now I'm stuck on trying to get an enemy to shoot projectiles towards me. I followed a guide on youtube but something is missing in my code. When I start the game, my enemy shoots one projectile towards me, while the following projectiles just spawns on his body instead of tracing to me. Enemy.cs: Code (CSharp): public class Enemy : MonoBehaviour { public float speed; public float stoppingDistance; public float retreatDistance; private float timeBtwShots; public float startTimeBtwShots; public GameObject projectile; public Transform player; void Start() { player = GameObject.FindGameObjectWithTag("Player").transform; timeBtwShots = startTimeBtwShots; } void Update() { if (Vector2.Distance(transform.position, player.position) > stoppingDistance) { transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime); } else if (Vector2.Distance(transform.position, player.position) < stoppingDistance && Vector2.Distance(transform.position, player.position) > retreatDistance) { transform.position = this.transform.position; } else if (Vector2.Distance(transform.position, player.position) < retreatDistance) { transform.position = Vector2.MoveTowards(transform.position, player.position, -speed * Time.deltaTime); } if (timeBtwShots <= 0) { Instantiate(projectile, transform.position, Quaternion.identity); timeBtwShots = startTimeBtwShots; } else { timeBtwShots -= Time.deltaTime; } } } Projectile.cs: Code (CSharp): public class Projectile : MonoBehaviour { public float speed; private Transform player; private Vector2 target; void Start() { player = GameObject.FindGameObjectWithTag("Player").transform; target = new Vector2(player.position.x, player.position.y); } void Update() { transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime); if (transform.position.x == target.x && transform.position.y == target.y) { DestroyProjectile(); } } void OnTriggerEnter2D(Collider2D other) { if (other.CompareTag("Player")) { DestroyProjectile(); } } void DestroyProjectile() { Destroy(gameObject); } }
This is some old code i had for shooting projectiles that aim towards the player. This code goes on the projectile itself, not the enemy. Not sure if it works exactly how you are wanting but here: Code (CSharp): public float moveSpeed = 4f; //public float fireBallSpeed = 7f; Rigidbody2D rb; Player target; Vector2 moveDirection; bool facingRight = false; public GameObject explosion; void Awake() { rb = GetComponent<Rigidbody2D>(); target = FindObjectOfType<Player>(); } // Use this for initialization void Start () { moveDirection = (target.transform.position - transform.position).normalized* moveSpeed; rb.velocity = new Vector2(moveDirection.x, moveDirection.y); Destroy(gameObject, 2f); if (target.transform.position.x > transform.position.x) { Flip(); } } private void Update() { if(target ==null) { return; } } private void OnTriggerEnter2D(Collider2D collision) { if(collision.gameObject.tag =="Player") { Instantiate(explosion, transform.position, transform.rotation); Debug.Log("Hit"); Destroy(gameObject); } if (collision.gameObject.tag == "ground") { Instantiate(explosion, transform.position, transform.rotation); Debug.Log("Hit the ground"); Destroy(gameObject); } } void Flip() { facingRight = !facingRight; Vector3 theScale = transform.localScale; theScale.x *= -1; transform.localScale = theScale; }
Do you have a spawn position for the projectiles to spawn at? They may be hitting the collider of the enemy and not going anywhere. Also, can you post snippets of the enemy's inspector and what is on your bullet prefab?
I made it work eventually, your code was great just had to change some variable names. Thanks for the help, much appreciated
So your gif is showing that the projectiles arent going towards the player and the inspector image on your enemy shows that the player field is empty as well. Drag the player onto that field and it should always shoot towards player.
If the FindObjectOfType<Player>() is returning null, you can use GameObject.Find("YourPlayerNameHere").GetComponent<Transform>() or do a FindObjectWithTag<Player>().
I just removed gravity from the projectiles and increased the movement speed a bit, now it works like a charm.