- Home /
Rest Rotation Constraint
Im trying to reset the X rotation when my gameobject moves past a certain transform position.For some reason this isnt working for me. Any work around?
function Update () {
if(transform.rotation.x > 2){
transform.rotation.x = 0;
}
}
Answer by aldonaletto · Aug 19, 2012 at 02:45 AM
transform.rotation is a quaternion, and its XYZ components aren't those nice angles you see in the Inspector/Rotation fields - these fields are actually the transform.eulerAngles (or transform.localEulerAngles, if the object is a child). You could try something like this:
function Update(){ if (transform.eulerAngles.x > 2){ transform.eulerAngles = Vector3(0,0,0); } }But notice that you can't rely on eulerAngles.x when the other elements (y and z) aren't zero: Unity calculates eulerAngles from transform.rotation, and may return some weird combinations (there are several possible Euler angles that result in the same rotation).
Also, you can't change only X or only Y value of a vector. Never write transform.position.x = 10, ins$$anonymous$$d create a whole new vector with transform.position = new vector3(10,0,0);
Yes, specially for eulerAngles/localEulerAngles: Unityscript accepts writing to a single component (x, y or z), but doing this may generate errors that after some repetitions may produce incredibly wrong results.
Your answer
Follow this Question
Related Questions
Move an object toward an angle in 2d space 0 Answers
Syncing Position and Rotation data in ECS tests? 0 Answers
Smooth reset rotation problem 1 Answer
Rotating Camera around player object 0 Answers