How do I get a bullet prefab's sprite to use the same rotation as my weapon?

144 Views Asked by At

I'm working on a top down concept where the gun revolves around the player and flips according to the side your crosshair is on (similar to ZERO Sievert). I'm trying to have my bullet sprite have the correct rotation when firing in relation to my players weapon.

Below is how I'm instantiating the bullet in a shooting script which fires the correct way but the sprite itself is not rotated correctly.

void Shoot()
    {
        GameObject bullet = Instantiate(bulletPrefab, firingPoint.position, firingPoint.rotation);      
        Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
        rb.AddForce(firingPoint.right * bulletForce, ForceMode2D.Impulse);  
    }

and in my weapon handling script this is my implementation of the weapon rotation, I am flipping the weapons y scale to correct the sprite for now.


private void FixedUpdate()
    {
        RotateWeapon();
        if (crosshair.transform.position.x < 0)
        {
            FlipWeapon();
        }
    }
void RotateWeapon()
    {
        float AngleRad = Mathf.Atan2(crosshair.transform.position.y - currentWeapon.transform.position.y, crosshair.transform.position.x - currentWeapon.transform.position.x);
        float AngleDeg = (180 / Mathf.PI) * AngleRad;
        currentWeapon.transform.rotation = Quaternion.Euler(0, 0, AngleDeg);
    }

    void FlipWeapon()
    {
        currentScale = transform.parent.localScale;
        currentScale.y *= -1;
        currentWeapon.transform.localScale = currentScale;
    }

I'm currently at a standstill on how to achieve this as most resources I've come across for top down shooting have the player turning up to a full 360 degrees where my player only faces left or right and the weapon itself only has a range of motion of 180 degrees on either side before its set to flip.

1

There are 1 best solutions below

0
Efe Ersoy On

Maybe you can try and add an offset so when you Instantiate the bullet it will look something like this:

GameObject bullet = Instantiate(bulletPrefab, firingPoint.position, firingPoint.rotation + offset);

here offset will be a rotational x y z coordinate so you would want to rotate it on the z coordinate (try all of them until you fin the right one) and rotate it either -90 degrees or 90 deegrees. You might have to rotate it 180 degress but dont use 270 instead use -90. Trust me it will help in your later days in coding. Also make sure to set offset as a variable:

private Vector3 offset = 0, 0, -90;

!Remember you can change the x y z coordinates!