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. Dismiss Notice

Resolved Annoying Problem with Pooled Objects

Discussion in 'Scripting' started by MaximumTre, Aug 23, 2020.

  1. MaximumTre

    MaximumTre

    Joined:
    Nov 28, 2015
    Posts:
    163
    I'm trying to get a simple third person projectile shooter script to work but for some reason the bullets are not going in the direction I want them to. This should be straightforward, but it doesn't work.

    The ShotPoint is changed in FixedUpdate, and is always pointed towards the center of the screen. It's working because I have a line render on it to check if it's pointing in the right direction, and it is.



    I was changing the next bullets transform before activating it, but that isn't working either. The position changes but the rotation does not. What am I doing wrong?

    The bullets movement script simply does a
    Code (CSharp):
    1. transform.Translate(transform.forward * moveSpeed * Time.fixedDeltaTime);
    and does not affect the rotation in any way. I'm also not using a Rigidbody so there aren't any physics forces being applied. Highly annoying problem.


    Code (CSharp):
    1. void FireNextBullet()
    2.         {
    3.             for (int i = 0; i < BulletPool.Count; i++)
    4.             {
    5.                 // Get the first bullet that isn't active and set it's transform to the shot point, then activate it.
    6.                 if (!BulletPool[i].activeInHierarchy)
    7.                 {
    8.  
    9.                     BulletPool[i].SetActive(true);
    10.                     BulletPool[i].transform.position = ShotPoint.position;
    11.                     BulletPool[i].transform.rotation = ShotPoint.rotation;
    12.  
    13.                     MuzzleFlash.Play();
    14.                     currentShotTime = 0;
    15.                     break;
    16.                 }
    17.             }
    18.         }
     
    Last edited: Aug 23, 2020
  2. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,920
    If you think it's a problem with how the pooled objects are set up, you could temporarily switch to an Instantiate. If that still has the problem, you know it's not the poolers fault.
     
  3. MaximumTre

    MaximumTre

    Joined:
    Nov 28, 2015
    Posts:
    163
    See, that's the weird thing, I already tried that and I get the same issue.

    Edit:

    EVEN WEIRDER: When I set the rotation to something like Vector3.zero THAT works! wth?
     
    Last edited: Aug 23, 2020
  4. MaximumTre

    MaximumTre

    Joined:
    Nov 28, 2015
    Posts:
    163
    This must be some kind of bug. I have made a child of the shot point at z = 100 local position and when I use Transform.LookAt on the bullet it still doesn't point towards that object.
     
  5. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,920
    That's not weird, it's the whole point. Some other part of your code is the problem -- not how you create the item.

    I never use Translate so it took me until now to see the problem: The movement command is wrong. Translate uses local coords. position+=Vector3.forward is the basic command to move forward along +z. To move _your_ forward, you can either use position+=transform.forward OR use translate(Vector3.forward). But using both together overcompensates.
     
    mopthrow likes this.
  6. MaximumTre

    MaximumTre

    Joined:
    Nov 28, 2015
    Posts:
    163
    I see. The tooltip should denote that it's local space unless otherwise stated, but I should've checked the documentation. Thanks for the help!