Bullet prefab not instantiated in the correct rotation


Can anybody explain how I can instantiate the bullet prefab at the correct rotation? It’s coming out vertically when it’s supposed to be a horizontal sprite.

Here’s my script if necessary

public class Shooting : MonoBehaviour
{
    public Transform firePoint;
    public GameObject bulletPrefab;

    [SerializeField] float bulletForce = 20f;

    void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            Shoot();
        }
    }

    void Shoot()
    {
        GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
        Rigidbody2D bulletRB = bullet.GetComponent<Rigidbody2D>();
        bulletRB.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);

        GetComponent<AudioSource>().Play();
    }
}

Looks like zero rotation on your gun is different from zero rotation on your bullet.

I usually address this by rotating the graphics inside the prefab, often by inserting an extra GameObject above it, then rotating the sprite beneath it. The hierarchy would look like:

BulletPrefab
    RotationToAlignSpriteProperly
        ActualGraphicalSpriteHere

This helped thanks!

1 Like

Another option is to change the rotation when you instantiate:

GameObject bullet = Instantiate(bulletPrefab, firePoint.position,
            Quaternion.Euler(firePoint.rotation.x, firePoint.rotation.y, firePoint.rotation.z + 90));
        // could also be -90, depends on your sprite's original orientation
1 Like