how would I set the rotation of an instantiated object?

I’m new to Unity and I’ve been stuck on this for two days and I’ve lost count of how many times I’ve considered throwing my laptop out the window. I desperately need some help with this. how would I go about taking the Y axis rotation of the object a script is applied to and instatiate an prefab within that script to face in the same direction. I’ve looked at forums and websites, searched through youtube tutorials but can’t find a simple and direct answer. The API has all the pieces but I can’t get my head around linking it all together. Thanks for taking the time to read my half question/half rant thing I’ve got going on here.

I, my friends, am an idiot. Before I don my dunce hat and sit myself in the corner of shame I'll just say that if you don't have the correct rotation set on your model before bringing it into Unity then Unity'll not behave itself when you rotate something based on the rotation of your wonky little asset. be warned! Now where did I leave that dunce hat?

3 Answers

3

Hi,

When you call the Instantiate function, you can set the rotation there. I’d recommend storing the rotation of a Prefab in a new variable:

GameObject CurrentPrefab;
Quaternion rotation = CurrentPrefab.transform.rotation;

Instantiate(CurrentPrefab, somePosition, rotation);

Hope this helps :slight_smile:

I look good in a dunce hat, it suits me.

you can assign instantiated objecto to some variable and then set it’s rotation.

public GameObject obj;

GameObject instantiatedObj = Instantiate(obj, transform.position, Quaternion.identity);

instantiatedObj.transform.rotation = Quaternion.Euler(0,45,0);