- Home /
Door Open With Object Pickup
hello I'm quite new so i apologize if i am asking a very easy or difficult question. my overall goal is to have a door that wont open, when you hit "e" it says "door wont open", then when i find a crowbar i hit "e" and then it disappears and then when i go back up to the door it will allow me to open it with the "e". my script for the door is:
// Smothly open a door var smooth = 2.0; var DoorOpenAngle = 90.0; var DoorCloseAngle = 0.0; var open : boolean; var enter : boolean;
//Main function function Update (){
if(open == true){ var target = Quaternion.Euler (0, DoorOpenAngle, 0); // Dampen towards the target rotation transform.localRotation = Quaternion.Slerp(transform.localRotation, target, Time.deltaTime * smooth); }
if(open == false){ var target1 = Quaternion.Euler (0, DoorCloseAngle, 0); // Dampen towards the target rotation transform.localRotation = Quaternion.Slerp(transform.localRotation, target1, Time.deltaTime * smooth); }
if(enter == true){ if(Input.GetKeyDown("e")){ open = !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){
} //@youtube.com/user/maksimum654321
Answer by Razacx · Oct 16, 2012 at 12:55 AM
First of all, I'm assuming that the code for rotating the door works as intended?
I modified your code quite a bit, here is the finished result:
// Smoothly open a door
var smooth = 2.0;
var DoorOpenAngle = 90.0;
var DoorCloseAngle = 0.0;
var inRange : boolean = null;
var unlocked : boolean = false;
var isOpen : boolean = false;
var opening : boolean = false;
var hasCrowBar : boolean = false; //Has to be changed from another script
//Main function
function Update (){
if(opening){
var target = Quaternion.Euler (0, DoorOpenAngle, 0);
// Dampen towards the target rotation
transform.localRotation = Quaternion.Slerp(transform.localRotation, target, Time.deltaTime * smooth);
}
if(!opening){
var target1 = Quaternion.Euler (0, DoorCloseAngle, 0);
// Dampen towards the target rotation
transform.localRotation = Quaternion.Slerp(transform.localRotation, target1, Time.deltaTime * smooth);
}
if(!isOpen) {
if(inRange){
if(Input.GetKeyDown("e")){
if(unlocked) {
opening = !opening;
}
if(!unlocked) {
if(hasCrowBar) {
unlocked = true;
Debug.Log("You have unlocked the door!");
}
if(!hasCrowBar) {
Debug.Log("The door won't open, please find a crowbar");
}
}
}
}
}
}
//Activate the Main function when player is near the door
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") {
inRange = true;
}
}
//Deactivate the Main function when player is go away from door
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Player") {
inRange = false;
}
}
Your answer
Follow this Question
Related Questions
Scripting issue with opening a door with a key. 2 Answers
Unlocking Gate with Key 1 Answer
Help with making his work with a key 1 Answer
Door opening with a key, my scripts and problem 1 Answer
Problems with key script 0 Answers