Bullets instantiating at the wrong places when moving left/right.

Hello everybody.

I have a unity project which is an FPS shooter. Everything is looking/going good to me except for one thing:
I am instantiating bullets at the position of an object called weaponHead which is a child object of my weapon. (you can see my hierarchy in the picture)
The problem is that: when my player moves right/left or rotates the mouse left/right fast the bullets instantiate at the wrong position. (it is like a force impacts them but it doesn’t have to).

When moving to the left, the bullet instantiates at the right, and when going right the opposite happens.
I have attached some photos and code snippets from my script. I would appreciate it so much if any of you can help to solve this.

Thanks before.

in the above photo I am moving right, and the bullet instantiates at left even though it has to appear in the center of the weaponHead


in the above photo you can see my hirearchy and the position of the weaponhead.

GameObject bullet = ObjectPool.SharedInstance.GetPooledObject();

if (bullet != null)

{

bullet.transform.position = weaponHead.transform.position;

bullet.transform.rotation = weaponHead.transform.rotation;

bullet.SetActive(true);

When moving to the left, the bullet instantiates at the right, and when going right the opposite happens.
I have attached some photos and code snippets from my script. I would appreciate it so much if any of you could help to solve this.

}

I would say you’re simply instantiating the bullets before the character moves each frame. This will cause bullets to be instantiated at the position the character was during the previous frame, not the position it is currently at.

The solution is simple: instantiate the bullets after moving the character. Eg. if you’re moving the character in Update(), instantiate the bullets in LateUpdate().

2 Likes

I tend to agree with @arkano22 on this point… I already have a blurb about it, but just putting the later thing into LateUpdate() is a fine fine fine way to just get 'er done.

Thanks for the information. But the thing is that: I am already doing so. My weaponController script(the script that instantiates bullets) use LateUpdate, while the playerMovement scriptuses update. :frowning: