making prefab has the same rotation as the cannon

Hi , i have a cannon that shoots a prefab but i want to make the prefab has the same rotation as the cannon , how can i do that with java script , thanx

If you are using the Instantiate function to create your prefab, then all you have to do is change the rotation parameter so that it is equal to the rotation of your cannon.


Instead of this:

      //Version shown in several YouTube tutorials - Ignores rotation
    Instantiate(prefab, cannon.transform.position, Quaternion.identity);

Use this:

      //The only difference is that rotation is taken into account.
    Instantiate(prefab, cannon.transform.position, cannon.transform.rotation);

The easiest way to achieve that is take your shoot script and attach it to a game object that you can use as a spawn point for the cannon ball and place it just in front of the cannon barrel and make the game object a child of whatever part of the cannon is rotating. then when you rotate, the spawn point will rotate with it.