This door script causing door to go crazy?

//Make an empty game object and call it “Door”
//Rename your 3D door model to “Body”
//Parent a “Body” object to “Door”
//Make sure thet a “Door” object is in left down corner of “Body” object. The place where a Door Hinge need be
//Add a box collider to “Door” object and make it much bigger then the “Body” model, mark it trigger
//Assign this script to a “Door” game object that have box collider with trigger enabled
//Press “f” to open the door and “g” to close the door
//Make sure the main character is tagged “player”

// Smothly open a door
var smooth = 2.0;
var DoorOpenAngle = 90.0;
var opening:AudioClip;
private var open : boolean;
private var enter : boolean;

private var defaultRot : Vector3;
private var openRot : Vector3;

function Start(){
defaultRot = transform.eulerAngles;
openRot = new Vector3 (defaultRot.x, defaultRot.y + DoorOpenAngle, defaultRot.z);

//Main function
function Update (){
//Open door
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth);
//Close door
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth);

if(Input.GetKeyDown("e") && enter){
open = !open;

function OnGUI(){
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 150, 30), "Press 'E' to open");

//Activate the Main function when player is near the door
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") {
enter = true;

//Deactivate the Main function when player is go away from door
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Player") {
enter = false;

What could be causing this:

It’s easy to replicate. I go to the door, open and close it a few times as normal, walk away, then come back and try to open it and this happens.

Here is what I believe is happening. When you read ‘transform.eulerAngles’, these angles are derived from the Transform.rotation, which is a Quaternion. The angles you get out are not necessarily the ones you expect, and are not even the ones you put in. For example, run this script:

transform.eulerAngles = Vector3(180,0,0);

The results will be (0,180,180). Can you see how the changing representation might cause havoc with your Slerp code?

You can approach a fix in a couple of ways. First, you can calculate Quaternions for ‘defaultRot’ and ‘openRot’ in Start(). Then you can use those Quaternions in Quaternion.Slerp() to open and close the door.

The second solution is to maintain your own Vector3 and never read eulerAngles. Or only once at the start. The Slerp() done with your Vector3 and then the results assigned to eulerAngles:

 theRotation = Vector3.Slerp(theRotation, openRot, Time.deltaTime * smooth);
 transform.eulerAngles = theRotation;

Thank you very much for your reply. I’m a little confused about how I should go about implementing your suggested changes though. If you would perhaps expand on that I would be extremely grateful. Thank you :slight_smile: