Search Unity

2D enemy shooting

Discussion in '2D' started by AntBagz, Mar 22, 2021.

  1. AntBagz

    AntBagz

    Joined:
    Mar 9, 2021
    Posts:
    6
    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):
    1. public class Enemy : MonoBehaviour
    2. {
    3.     public float speed;
    4.     public float stoppingDistance;
    5.     public float retreatDistance;
    6.  
    7.     private float timeBtwShots;
    8.     public float startTimeBtwShots;
    9.  
    10.     public GameObject projectile;
    11.  
    12.     public Transform player;
    13.  
    14.     void Start()
    15.     {
    16.         player = GameObject.FindGameObjectWithTag("Player").transform;
    17.         timeBtwShots = startTimeBtwShots;
    18.     }
    19.  
    20.  
    21.     void Update()
    22.     {
    23.         if (Vector2.Distance(transform.position, player.position) > stoppingDistance)
    24.         {
    25.             transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
    26.         }
    27.  
    28.         else if (Vector2.Distance(transform.position, player.position) < stoppingDistance && Vector2.Distance(transform.position, player.position) > retreatDistance)
    29.         {
    30.             transform.position = this.transform.position;
    31.         }
    32.         else if (Vector2.Distance(transform.position, player.position) < retreatDistance)
    33.         {
    34.             transform.position = Vector2.MoveTowards(transform.position, player.position, -speed * Time.deltaTime);
    35.         }
    36.  
    37.         if (timeBtwShots <= 0)
    38.         {
    39.             Instantiate(projectile, transform.position, Quaternion.identity);
    40.             timeBtwShots = startTimeBtwShots;
    41.  
    42.         }
    43.         else
    44.         {
    45.             timeBtwShots -= Time.deltaTime;
    46.         }
    47.     }
    48. }
    Projectile.cs:
    Code (CSharp):
    1. public class Projectile : MonoBehaviour
    2.  
    3. {
    4.     public float speed;
    5.  
    6.     private Transform player;
    7.     private Vector2 target;
    8.    
    9.     void Start()
    10.     {
    11.         player = GameObject.FindGameObjectWithTag("Player").transform;
    12.         target = new Vector2(player.position.x, player.position.y);
    13.     }
    14.  
    15.    
    16.     void Update()
    17.     {
    18.         transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
    19.         if (transform.position.x == target.x && transform.position.y == target.y)
    20.         {
    21.             DestroyProjectile();
    22.         }
    23.     }
    24.  
    25.     void OnTriggerEnter2D(Collider2D other)
    26.     {
    27.         if (other.CompareTag("Player"))
    28.         {
    29.             DestroyProjectile();
    30.         }
    31.     }
    32.     void DestroyProjectile()
    33.     {
    34.         Destroy(gameObject);
    35.     }
    36. }
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,466
    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):
    1. public float moveSpeed = 4f;
    2.     //public float fireBallSpeed = 7f;
    3.     Rigidbody2D rb;
    4.     Player target;
    5.     Vector2 moveDirection;
    6.     bool facingRight = false;
    7.     public GameObject explosion;
    8.  
    9.     void Awake()
    10.     {
    11.         rb = GetComponent<Rigidbody2D>();
    12.         target = FindObjectOfType<Player>();
    13.     }
    14.     // Use this for initialization
    15.     void Start ()
    16.     {      
    17.         moveDirection = (target.transform.position - transform.position).normalized* moveSpeed;
    18.         rb.velocity = new Vector2(moveDirection.x, moveDirection.y);
    19.         Destroy(gameObject, 2f);
    20.  
    21.         if (target.transform.position.x > transform.position.x)
    22.         {
    23.             Flip();
    24.         }
    25.     }
    26.  
    27.     private void Update()
    28.     {
    29.         if(target ==null)
    30.         {
    31.             return;
    32.         }
    33.     }
    34.  
    35.     private void OnTriggerEnter2D(Collider2D collision)
    36.     {
    37.         if(collision.gameObject.tag =="Player")
    38.         {
    39.             Instantiate(explosion, transform.position, transform.rotation);
    40.             Debug.Log("Hit");
    41.             Destroy(gameObject);
    42.         }
    43.         if (collision.gameObject.tag == "ground")
    44.         {
    45.             Instantiate(explosion, transform.position, transform.rotation);
    46.             Debug.Log("Hit the ground");
    47.             Destroy(gameObject);
    48.         }
    49.  
    50.     }
    51.     void Flip()
    52.     {
    53.         facingRight = !facingRight;
    54.         Vector3 theScale = transform.localScale;
    55.         theScale.x *= -1;
    56.         transform.localScale = theScale;
    57.     }
     
  3. AntBagz

    AntBagz

    Joined:
    Mar 9, 2021
    Posts:
    6
    Same issue even with ur code :( the bullets just spawns behind the enemy body.. I'm lost.
     
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,466
    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?
     
  5. AntBagz

    AntBagz

    Joined:
    Mar 9, 2021
    Posts:
    6
    I made it work eventually, your code was great just had to change some variable names. Thanks for the help, much appreciated :)
     
    Last edited: Mar 23, 2021
  6. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,466
    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.
     
    AntBagz likes this.
  7. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,466
    If the FindObjectOfType<Player>() is returning null, you can use GameObject.Find("YourPlayerNameHere").GetComponent<Transform>() or do a FindObjectWithTag<Player>().
     
  8. AntBagz

    AntBagz

    Joined:
    Mar 9, 2021
    Posts:
    6
    I just removed gravity from the projectiles and increased the movement speed a bit, now it works like a charm.
     
    Cornysam likes this.