Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Projectile moving towards the wrong direction

Discussion in 'Scripting' started by busterlock, Feb 25, 2022.

  1. busterlock

    busterlock

    Joined:
    Sep 26, 2015
    Posts:
    58
    [SOLVED]

    Hi!

    I have a script which enqueues projectiles, and when the player clicks the mouse button it dequeues on of these projectiles and aims it towards the mouse direction. The big problem is that sometimes the direction is turning out to be random and absolutely not towards the forward direction of the parent's transform. I try looking for some solutions across the forum but couldn't find it.

    My code for instantiating the projectiles:

    Code (CSharp):
    1.     public GameObject bullet;
    2.     private Queue<GameObject> bulletQueue = new Queue<GameObject>();
    3.  
    4.     private void Start()
    5.     {
    6.         //we're going to put the bullets inside the queue
    7.         //so to start right up with them.
    8.         for (int i=0; i < 10; i++)
    9.         {
    10.             GameObject current_bullet = Instantiate(bullet);
    11.             current_bullet.SetActive(false);
    12.             bulletQueue.Enqueue(current_bullet);
    13.         }
    14.     }
    15.  
    16.     private void Update()
    17.     {
    18.         if (Input.GetButtonDown("Fire1"))
    19.         {
    20.             //get the current position of the gun and instantiate the bullet at this position
    21.             Vector3 current_position = transform.position;
    22.             Quaternion current_rotation = transform.rotation;
    23.             //dequeue this bullet and then put his new position as being the gun's position
    24.             GameObject _current_bullet = bulletQueue.Dequeue();
    25.             _current_bullet.transform.position = current_position;
    26.             _current_bullet.transform.rotation = current_rotation;
    27.             _current_bullet.SetActive(true);
    28.             //after we have enquede it, now we have to return it to its original place
    29.             bulletQueue.Enqueue(_current_bullet); //will this work?
    30.         }
    31.     }
    32. }
    And also the code that actually gets the projectiles to be shot:

    Code (CSharp):
    1.     private void Update()
    2.     {
    3.         if (timer >= 0)
    4.         {
    5.             timer -= Time.deltaTime; //we decrease each frame rate the timer so the bullet deactivates
    6.         }
    7.         else
    8.         {
    9.             this.gameObject.SetActive(false);
    10.         }
    11.     }
    12.  
    13.     private void OnEnable()
    14.     {
    15.         parentObj = GameObject.FindGameObjectWithTag("GunBarrel");
    16.         this.gameObject.GetComponent<Rigidbody>().AddForce(parentObj.transform.forward * speed);
    17.     }
    18.  
    19.     private void OnDisable()
    20.     {
    21.         timer = 1.5f; //we restart it back to the same timer
    22.     }
    23.  
    24.     private void OnCollisionEnter(Collision collision)
    25.     {
    26.         if (collision.gameObject.CompareTag("Bullet"))
    27.         {
    28.             Debug.Log("Hitting bullet");
    29.         }
    30.     }
    31. }
    At the current stage I already cheked if transform.forward changed, but it didn't. I also checked if there was any collisions, and there wasn't, and the instantiating point of my projectiles is outside of the gun, in an far enough area where new projectiles won't hit each other.

    The game's camera is isometric, and the shooting direction is detected by the mouse.

    Thank you for your help.
     
    Last edited: Feb 26, 2022
  2. MvNimwegen

    MvNimwegen

    Joined:
    Dec 19, 2019
    Posts:
    57
    I am trying to understand what you are doing here but I seem to not get a good grasp on it.

    Maybe watch this tutorial. It could help.
     
  3. busterlock

    busterlock

    Joined:
    Sep 26, 2015
    Posts:
    58
    OK, for anyone trying to use object pooling which may face the same problem that I did, which is basically, once you start reusing the same projectile it spawns towards a wrong direction, before enqueuing the item back to the queue, remember to restart its rigidbody.velocity, otherwise it will be respawned while still having the velocity from the last use.
     
    amaakir_unity likes this.