- Home /
Door rotate on hinge
I've got a script which I got from "xrunesx"on youtube of opening a door when you have a key collected. I've changed the script so that it rotates rather than is transformed up the Y axis, but it rotates in the middle. I wanted it to rotate on a hinge, which I thought all I'd have to do is add in an empty and make that the parent of the door, but it won't work. Can someone help me please?
static var gotKey : boolean = false; // the var which u want to call for key script
var smooth = 2.0;
var doorOpenY :float;
var doorCloseY :float;
var open : boolean = false;
var enter : boolean = false;
var Smooth : float = 1.5;
var OpenAngle : float = 90.0;
var CloseAngle : float = 0.0;
var OpenDoor : boolean = false;
var EnterTrigger : boolean = false;
function Update (){
if(open == true){
var DoorOpen = Quaternion.Euler(0, OpenAngle, 0);
transform.localRotation = Quaternion.Slerp(transform.localRotation, DoorOpen, Time.deltaTime * Smooth);
}
if(open == false){
var DoorClosed = Quaternion.Euler(0, CloseAngle, 0);
transform.localRotation = Quaternion.Slerp(transform.localRotation, DoorClosed, Time.deltaTime * Smooth);
}
//the part for pressing smt to open
if(gotKey && enter){
if(Input.GetKeyDown("e")){
open = true;
GUIkey.guiEnable = false;
print("You don't have key anymore");
}
}
}
//in here you just call if Player enter the trigger is true
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") {
enter = true;
print("insert key");
}
}
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Player") {
enter = false;
print("go to door");
}
}
// written by Ali Hashemi
// www.lyasstudio.wordpress.com
Answer by Lovrenc · Jan 13, 2013 at 12:53 PM
You are on the right track.
Cou can put an empty as a parent to the door. Make sure you put it at the hinge position AND to call the rotation code ON THE EMPTY ITSELF.
You could also make a door in your modeling program (blender, 3ds MAX, ....) and put pivot in the correct position. Pivot (origin) determines the point around which all transformations happen.
You can try this script that enables you to change pivot inside editor easily.