How to make bullet face away from the gun barrel when shot in side-view 2D/3D shooter?

I tried lots of things but I still can’t get this simple thing right.

I just want to make the bullet shot from the gun to face away from the gun barrel but I can’t understand eulers and no solution I found on the internet works, even most of them being for top-down shooters.

The bullet just shoots with the same rotation no matter where I aim it.

queasypoliteamericantoad

My Shoot function:

public void Shoot(Vector3 playerpos)
{
           
            Quaternion bulletRotation = Quaternion.Euler(0, 0, this.transform.rotation.x - 90);
            GameObject bullet = GameObject.Instantiate(thisWeapon.projectile, bulletSpawn.position, bulletRotation); // TO-DO: fix bullet rotation
            Rigidbody bBody = bullet.GetComponent<Rigidbody>();
            bBody.AddForce(bulletSpawn.right * thisWeapon.projectileSpeed * Time.fixedDeltaTime, ForceMode.Impulse);
            bullets.Add(bullet);
            ++global_vars.global_bulletCount;
            return;
 }

The script is placed on the gun and the bullet is spawned on a child object of the gun (bulletSpawn), the bullet shoots perfectly, only issue is the bullet rotation.

I hope this is enough information, I’m probably committing a simple error, thank you in advance.

Just to clarify the problem (because I just realized the gif looks like shit), the bullet spawned is rotated 90 degrees to the right no matter where I point the gun when I want it to be rotated 90 degrees to the right relative to the gun barrel.

I would ensure the same axis is pointing forward for both the gun and bullet (usually z axis, but all that is important here is that both have the same axis). Then you just set the bullet transform rotation to the same as the gun transform rotation.

If they don’t end up pointing the same direction, that means the bullet and the gun don’t have the same axis pointing forward. So you fix that, often by just giving the problem object a parent and then orienting the child so the parent’s z axis actually points forward when the model appears to be pointing forward.

2 Likes