- Home /
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)?
Answer by KayelGee · Oct 07, 2014 at 12:22 PM
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);
}
...
I'll try coding it in like you said. I tried changing it in the inspector before and ins$$anonymous$$d of moving in the opposite direction it started spinning around like a hurricane.
If it spins out of control this might be because once he reaches -1 he'll turn that into 359 degrees so -90 isn't a reachable rotation here. 270 on the other hand will make the door go the long way because Vector3.Slerp doesn't treat the values as rotations. I didn't think this thorugh because your script rotates with Vectors ins$$anonymous$$d of Quaternions.
A solution to that would be to calculate the rotation with Quaternions and Quaternion.Slerp. I'll update my answer once I get to my Unity PC.
Your answer
Follow this Question
Related Questions
Writing my own enter/exit car - need help. 2 Answers
trigger is not working 0 Answers
How do I check a boolean from another script in C#? 0 Answers
How to delay a line of code. 2 Answers
object rotates when moving backward 1 Answer