- Home /
 
Door script not working when door is turned 90 degrees
I use the following code for doors. These doors have 0=x 0=y 0=z rotation, but when I turn the door 90 degrees on the y axis, the door doesn't open. What am I doing wrong?
 var door: Transform;
 
 var angleOpen: int;
 
 var angleClose: int;
 
 var speedOpen: int = 1000;
 
 function OnTriggerStay(other:Collider){
 
 if (door.transform.localEulerAngles.y < angleOpen){
 
 door.transform.Rotate(Vector3.up*Time.deltaTime*speedOpen);
 
 }
 
 }
 
              If you rotate the door, it looks like you'll need to adjust angleOpen and angleClose to compensate.
 As an alternative, you could just code in a value indicating how far the door should open, which you use to calculate appropriate angleOpen and angleClose during Start(). 
Answer by aldonaletto · Dec 06, 2013 at 03:58 AM
Don't trust in the values read from localEulerAngles or eulerAngles: there are many XYZ combinations that may be returned by the same rotation, and the "wrong" one may screw your code up. Do the opposite: keep the desired angle in a variable and "rotate" it mathematically, assigning the result to the actual localEulerAngles every frame:
 var door: Transform;
 var angleOpen: int;
 var angleClose: int;
 var speedOpen: float = 90; // degrees per second
 
 private var curAngle;
 
 function Start(){
   curAngle = angleOpen; // initialize curAngle
   door.transform.localEulerAngles = Vector3(0, curAngle, 0);
 }
  
 function OnTriggerStay(other:Collider){
   // "rotate" curAngle towards angleOpen
   curAngle = Mathf.MoveTowards(curAngle, angleOpen, Time.deltaTime * speedOpen);
   // assign it to the door transform
   door.transform.localEulerAngles = Vector3(0, curAngle, 0);
 }
 
              Yeah I really like this a lot more than my previous code! I'll try it out!
Your answer
 
             Follow this Question
Related Questions
Jump Script, no luck getting it to work.. 2 Answers
Pickup script not workimg 0 Answers
Walking script not working need help 1 Answer