Search Unity

Bullet not following player at given frame

Discussion in '2D' started by Tidcy, May 6, 2021.

  1. Tidcy

    Tidcy

    Joined:
    Apr 15, 2020
    Posts:
    53
    hello I have an enemy that instantiates bullets with a rate, and on the bullet I have this script
    Code (Csharp):
    1.  
    2. void Start()
    3.  
    4. {
    5.  
    6.    bee = Gameobject.Find(“bee”);
    7.  
    8.    rb.velocity = (bee.transform.position - transform.position).Normalized * speed;
    9.  
    10. }
    11.  
    But when the bullet appears it just stays static and doesn’t move am I doin something wrong? Thanks in advance
     
  2. KalOBrien

    KalOBrien

    Administrator

    Joined:
    Apr 20, 2021
    Posts:
    89
    So you need to have the :
    Code (CSharp):
    1.  rb.velocity = (bee.transform.position - transform.position).Normalized * speed;
    Inside of an Update method.
    At the moment you only have it targeting the bee and only at the very start of instantiation moving towards the target.
     
  3. Tidcy

    Tidcy

    Joined:
    Apr 15, 2020
    Posts:
    53
    Wouldn’t that make it follow the player constantly? Thx :)
     
  4. KalOBrien

    KalOBrien

    Administrator

    Joined:
    Apr 20, 2021
    Posts:
    89
    Oh my mistake I thought you wanted to track it like a missile.

    I'd take out the Normalized part of your code and increase the speed to something like 5 and it should shoot towards your bee target.

    Code (CSharp):
    1.     public GameObject bee;
    2.     public int speed = 3;
    3.     Rigidbody rb;
    4.     void Start(){
    5.         rb = this.GetComponent<Rigidbody>();
    6.    
    7.         bee = GameObject.Find("bee");
    8.  
    9.         rb.velocity = (bee.transform.position-transform.position) * speed;
    10.  
    11.     }