- Home /
More specific Quaternion.LookRotation
I have a gun script set up so that when you shoot something a bullet hole will appear the code:
Quaternion.LookRotation(hit.normal)
but i want the bullet to have a random rotation on the Y axis. not sure how to do it and was hoping someone can help. thanks in advance.
Answer by Bunny83 · Aug 21, 2016 at 01:04 AM
Quaternion.LookRotation actually has two parameters. The first parameter is the major-axis, the z-axis. The second parameter is a hint direction vector which defines the desired up direction. If you don't pass a direction vector for the up direction it default so Vector3.up. So the rotation will always be oriented in a way so the y axis is closest to the world y axis.
If you just want to randomly rotate the object around the z axis you can use Quaternion.AngleAxis to create a random rotation around the z axis and multiply the result of Quaternion.LookRotation with that one. Rotating around a different axis than the z-axis wouldn't make much sense since it would change the result of LookRotation so it won't face that direction anymore.
Quaternion.AngleAxis(Random.Range(0f,360f),hit.normal) * Quaternion.LookRotation(hit.normal);
Your answer
Follow this Question
Related Questions
LookRotation Vector3 is Zero, Yet Slerp Still Rotates? 2 Answers
Confused with LookRotation in C# 1 Answer
Turret rotation on one axis problems 2 Answers
Rotate object towards another object without using the local Y axis 0 Answers
How many degrees must object turn in y-axis to face target? 1 Answer