- Home /
Random Rotation in Z axis (C#)
Hi, I was trying to make a random rotation in z axis, I saw something about Euler Angles, Quaternions and Slerp but I have no idea of where to start, I hope you can help me. thanks.
Answer by rutter · Sep 14, 2014 at 11:49 PM
Unity uses quaternions to represent rotation internally. It's sometimes difficult to work with quaternions directly, so there are helper functions to translate to/from Euler angles, which people usually find more intuitive.
For example, you could read the Euler rotation, assign a random value to one axis, and then assign a new rotation based on that:
function Start() {
var euler = transform.eulerAngles;
euler.z = Random.Range(0.0, 360.0);
transform.eulerAngles = euler;
}
For more information, you can check out the Quaternion class in the scripting doc.
thanks! but could you show me how to do it in C#, I'm not sure how to add the euler
void Start() {
Vector3 euler = transform.eulerAngles;
euler.z = Random.Range(0f, 360f);
transform.eulerAngles = euler;
}
You can add vectors together, or multiply vectors by some scalar, but Unity doesn't necessarily react well if you try to assign rotations greater than 360 degrees via Euler angles.
How would you do that for an instantiate function? The code I have for that is:
Instantiate(projectile, firePoint.position, Quaternion.Euler(0, 0, Random.Range($$anonymous$$inSpread, $$anonymous$$axSpread)));
but unfortunately, I'm doing mouse ai$$anonymous$$g, and it always fires to the right.
@AcidmanRPGz like this
Instantiate(projectile, firePoint.position, Quaternion.Euler(new Vector3(0, 0, Random.Range($$anonymous$$inSpread, $$anonymous$$axSpread))));
Your answer

Follow this Question
Related Questions
Rotation Lerp 2 Answers
Quaternion from US to C# 1 Answer
Need help with storing Rotation in a Quaternion 1 Answer
Fixing the rotation around a particular axis using Quaternions? 2 Answers
Quaternion, Eulers and Problems 1 Answer