Door Opening

I was looking for a simple non animation way to open a door and found this bit of code:


// Smoothly open a door
var smooth = 2.0;
var DoorOpenAngle = 90.0;
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 (){
	if(open){
	//Open door
	transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth);
	}
	else{
	//Close door
	transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth);
	}

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

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

//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;
		}
}

I was wondering how I would go around making the door turn in the anticlockwise direction, since I have double doors and wish to open both at the same time.

I was thinking it had something to do with the defaultRot and OpenRot or the Euler Angle. Can you have a -Vector3(Negative Vector3)?

It’s pretty straightforward you just have to change the DoorOpenAngle to -90. You can also change the script like this:

var DoorOpenAngle : float = 90.0;

This should enable you to change the value inside the Inspector. This should work as expected, as long as your rotation never gets <0 because at this point funny things might happen.

To be safe on all fronts you have to adjust the script so it rotates with Quaternions instead of Vectors which is a simple edit. Here are the changes you have to make

...
private var defaultRot : Quaternion;
private var openRot : Quaternion;
 
function Start(){
    defaultRot = transform.rotation;
    openRot = defaultRot * Quaternion.Euler(0, DoorOpenAngle, 0);
}
 
//Main function
function Update (){
    if(open){
    //Open door
    	transform.rotation = Quaternion.Slerp(transform.rotation, openRot, Time.deltaTime * smooth);
    }
    else{
    //Close door
    	transform.rotation = Quaternion.Slerp(transform.rotation, defaultRot, Time.deltaTime * smooth);
    }
...