Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How should I make an enemy's bullet continue past the player's last position?

Discussion in '2D' started by Doctor_Fork, Nov 27, 2020.

  1. Doctor_Fork

    Doctor_Fork

    Joined:
    Apr 12, 2020
    Posts:
    4
    Hi, I'm making a top down shooter game right now and I'm currently working on a ranged enemy. The enemy should shoot at the player, and it does, but the projectile fires at the player's last position and stops when it gets there. I want it to continue past. I have 2 scripts here, one for the enemy itself and the other for the projectile:
    Code (CSharp):
    1. {
    2.     public float speed;
    3.     public float stoppingDistance;
    4.     public float retreaDistance;
    5.     public float bulletForce = 20f;
    6.  
    7.     private float timeBtwShots;
    8.     public float startTimeBtwShots;
    9.  
    10.     public Transform player;
    11.     public Transform firePoint;
    12.     public GameObject projectile;
    13.  
    14.     void Start()
    15.     {
    16.         player = GameObject.FindGameObjectWithTag("Player").transform;
    17.         timeBtwShots = startTimeBtwShots;
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.  
    24.         Vector3 direction = player.position - transform.position;
    25.         direction.z = -100000;      
    26.         transform.rotation = Quaternion.LookRotation(direction, Vector3.forward);
    27.  
    28.         if (Vector2.Distance(transform.position, player.position) > stoppingDistance)
    29.         {
    30.             transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
    31.         }
    32.         else if (Vector2.Distance(transform.position, player.position) < stoppingDistance && Vector2.Distance(transform.position, player.position) > retreaDistance)
    33.         {
    34.             transform.position = this.transform.position;
    35.         }
    36.         else if (Vector2.Distance(transform.position, player.position) > retreaDistance)
    37.         {
    38.             transform.position = Vector2.MoveTowards(transform.position, player.position, -speed * Time.deltaTime);
    39.         }
    40.  
    41.         if (timeBtwShots <= 0)
    42.         {
    43.             Shoot();
    44.             timeBtwShots = startTimeBtwShots;
    45.         }
    46.         else
    47.         {
    48.             timeBtwShots -= Time.deltaTime;
    49.         }
    50.         void Shoot()
    51.         {
    52.             GameObject bullet = Instantiate(projectile, firePoint.position, firePoint.rotation);
    53.             Rigidbody2D rb = projectile.GetComponent<Rigidbody2D>();
    54.             rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
    55.         }
    56.     }
    And the projectile one:
    Code (CSharp):
    1. {
    2.  
    3.     public float speed;
    4.     public GameObject hitEffect;
    5.  
    6.     private Transform player;
    7.     private Vector2 target;
    8.  
    9.     void Awake()
    10.     {
    11.  
    12.         player = GameObject.FindGameObjectWithTag("Player").transform;
    13.  
    14.         target = new Vector2(player.position.x, player.position.y);
    15.  
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
    21.     }
    22.  
    23.     void OnCollisionEnter2D(Collision2D collision)
    24.     {
    25.         GameObject effect = Instantiate(hitEffect, transform.position, Quaternion.identity);
    26.         Destroy(effect, 5f);
    27.         Destroy(gameObject);
    28.     }
    29.  
    30.    
    31. }
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Just replace your
    MoveTowards
    with a
    transform.Translate
    instead.

    MoveTowards
    is meant to move an object towards a specific position in the scene, and will stop the object after reaching the position. For a bullet though, you likely only want it to move forwards "forever", not reaching any specific position.
    transform.Translate
    will do just that.
     
    Doctor_Fork likes this.
  3. Doctor_Fork

    Doctor_Fork

    Joined:
    Apr 12, 2020
    Posts:
    4
    Where do I put the transform.Translate? It shows as an error if I replace MoveTowards with it because it doesn't like the Vector2.
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    In your projectile script, instead of this:
    Code (CSharp):
    1. transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
    You would do this:
    Code (CSharp):
    1. transform.Translate(Vector2.up * speed * Time.deltaTime);
    Passing
    Vector2.up
    in
    transform.Translate
    is likely what you're looking for. That will make the GameObject move upwards on the Y-axis relative to its rotation.

    You can use whatever other vector you need if
    Vector2.up
    isn't the direction you want the projectile to travel, however.

    See: https://docs.unity3d.com/ScriptReference/Transform.Translate.html
     
  5. Doctor_Fork

    Doctor_Fork

    Joined:
    Apr 12, 2020
    Posts:
    4
    Ok, thanks for the clarification!