- Home /
Setting rotation when instatiating a object
This is my existing code for my random gen for trees and rocks but I want to make it so every object gets a random rotation and I cannot figure this out.
Thanks!
Answer by MacDx · Jun 28, 2018 at 04:58 PM
You're already setting rotation while instantiating those objects. Quaternion.Identity (which means rotation at 0 x, 0 y and 0 z) is the rotation you're setting all your instantiated objects to. So instead of Quaternion.Identity pass a different quaternion.
Use something like this:
//Example
//Random rotation on the Y axis, I'm assuming that's what you want but you could
//easily do this for the other axes as well.
var tree = Instantiate(Tree, new Vector3(bla, bla, bla), Quaternion.Euler(0,Random.Range(0,360f),0));
Hope this helps!
Note: On future posts, please, instead of taking a picture of the code, post the text right here using the code sample button. That way is a lot easier for people to help. Regards
Your answer
![](https://koobas.hobune.stream/wayback/20220612165650im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotating a selected object 2 Answers
Mathf.MoveTowardsAngle is not working as predicted 1 Answer
change just z axis angle 1 Answer
My marble (Player/Gameobject) goes through the "play board" (gameobject). Why? 1 Answer
How to keep a Gameobject in the same position after a transform.Rotate? 2 Answers