Problems with Instantiate rotation

Hi! I am trying to instantiate a rocket prefab at the tip of my player’s rocket launcher. everything works fine, except the rotation of the rocket is throwing off the launch direction. The rocket spawns rotated up 90 degrees.

Is there any way to subtract 90 degrees from the transform.rotate? Or is there a different solution?

This is my code:

public float offset;
public GameObject projectile;
public Transform shotPoint;
private float timeBtwShots;
public float startTimeBtwShots;

void Update()
{
    Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);
    if(timeBtwShots <= 0 )
    {
        if (Input.GetMouseButtonDown(0))
        {
            Instantiate(projectile, shotPoint.position, transform.rotation);
            timeBtwShots = startTimeBtwShots;
        }
    }
    else
    {
        timeBtwShots -= Time.deltaTime;
    }
}

The rocket prefab looks like this:

it can often help a lot to create a very simple test that doesn’t have any shooting, cooldown, shotPoint, instantiation, etc.
Just do this:

  • Create a Test scene.

  • Create a Player gameobject which only displays a Sprite (no Rigidbody and don’t use any Prefabs if they contain script components).

  • Create a Rocket gameobject which only displays a Sprite (no Rigidbody and don’t use any Prefabs if they contain script components).

  • Create a TestRotation component which has a Transform rocketTransform field and connect it to the Rocket gameobject via the Inspector.

  • Add the TestRotation component to the Player gameobject.

  • Give the TestRotation script an Update method which simply points the Player sprite towards the mouse and then sets rocketTransform.rotation = transform.rotation (the rocket inherits the player’s rotation).
    No rotation offset.
    No instantiation.

What you should have now is two sprites that do not move (translate) but only rotate, and there is no collision detection whatsoever.

When you move the mouse the player sprite is pointed towards the mouse and then the rocket inherits the player’s rotation.

This test should make it clear in which direction the rocket faces when it inherits the player’s rotation, without anything else affecting the rocket’s rotation (like a Rigidbody colliding with the object that spawns it, etc.).
You will also know exactly how many degrees to add or subtract relative to the player sprite. Modify your TestRotation script and add the required rotation offset.

If your real scripts don’t behave like the test behaves even though you’re using the same rotation offset, keep the code that instantiates the rocket, but disable collision detection (you can simply enable isKinematic on the rigidbodies of both the player and the rocket if you’re using rigidbodies, of course).

If that doesn’t result in a rocket that points in the correct direction upon instantiation, does the rocket have a move script? If so, does the script in any way change the rotation of the rocket? If it does then temporarily disable whatever code is rotating the rocket and test again.

If I’m way off base, please tell me more about your setup.

I tried Instantiate(projectile, shotPoint.position, transform.rotation - 90); but it wouldn’t allow the
-90. How am I supposed to add an offset to the rotation?

transform.rotation is a Quaternion.
To rotate aQuaternion you have to multiply it by another Quaternion.

Instantiate(
    projectile, 
    shotPoint.position, 
    transform.rotation * Quaternion.Euler(0, 0, -90)
); 

I will try that. Thank you so much for all your help!

It worked! Thak you again for all the help!