- Home /
 
Problem is not reproducible or outdated
Lock a rotation
Hi,
Is it at all possible to make the rotation of an axis PERMANENTLY zero while still being able to have the other two actively rotating?
Answer by Dreamer · Jun 08, 2011 at 01:41 AM
You can lock the rotation of 1 axis by:
 function Update(){
  
 
   //end of script
   transform.eulerAngles.x=0;
 }
 
               transform.eulerAngles.x=0; This is wrong, you can't modify just one angle, you have to modify the whole object. transform.eulerAngles=new Vector3(0,transform.eulerAngles.y,transform.eulerAngles.z); 
@rajat No, I'm pretty sure putting ".x" at the end will allow you to edit just one axis. Adding all that stuff is just unnecessary fluff.
Negative @rct3fan24 That causes the following error:
 error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.eulerAngles'. Consider storing the value in a temporary variable
 
                  in Unity and
 Error    1    Cannot modify the return value of 'UnityEngine.Transform.eulerAngles' because it is not a variable
 
                  in Visual Studio.
@rajat is correct.
For future posterity, in UnityScript, you can edit a single axis by saying
  transform.eulerAngles.x = 0;
 
                  However, in C#, you have to assign/store the value in a Vector3, such as
 Vector3 blah = new Vector3
 (transform.eulerAngles.x,
  transform.eulerAngles.y,
  transform.eulerAngles.z);
 
                  Why? I don't know. I also don't know why everyone assumes that everybody programs in C#.