- Home /
The question is answered, right answer was accepted
Door Open on keydown and Close Door on same keydown?
yeah like you see in the Q titel i need to change the script a little.
now the behaviour on the door is F to open and then the door closes by himself so how do i change this so when i open it then it gonna be still open until I press F again to close it.
(easier said, that I do not want that door closes automatiskt, for I have to close the door on the same key-(F)
but atfer then I want to put in so the door closes aoutomatiskt
for we say that when a player opens a door and leave it open and run away, then I want that door to close automatically, we say after 1h. or the player can stay there and wait 1h and the door close, he actually don't need to run away but you get the point :)
Script:
#pragma strict
var theDoor : Transform;
private var drawGUI = false;
private var doorIsClosed = true;
function Update ()
{
if (drawGUI == true && Input.GetKeyDown(KeyCode.F))
{
changeDoorState();
}
}
function OnTriggerEnter (theCollider : Collider)
{
if (theCollider.tag == "Player")
{
drawGUI = true;
}
}
function OnTriggerExit (theCollider : Collider)
{
if (theCollider.tag == "Player")
{
drawGUI = false;
}
}
function OnGUI ()
{
if (drawGUI == true)
{
GUI.Box (Rect (Screen.width*0.5-51, 200, 102, 22), "Press F to open");
}
}
function changeDoorState ()
{
if (doorIsClosed == true)
{
theDoor.animation.CrossFade("Open");
//theDoor.audio.PlayOneShot();
doorIsClosed = false;
theDoor.animation.CrossFade("Close");
//theDoor.audio.Play();
doorIsClosed = true;
}
}
ThX in advance
Answer by ShadyProductions · Aug 02, 2014 at 03:27 PM
You can try something like this
//add this in your update function
if (Input.GetKeyDown(KeyCode.F))
{
changeDoorState();
}
function changeDoorState ()
{
if (doorIsClosed)
{
theDoor.animation.CrossFade("Open");
//theDoor.audio.PlayOneShot();
doorIsClosed = false;
}
else
{
theDoor.animation.CrossFade("Close");
//theDoor.audio.Play();
doorIsClosed = true;
}
}
hey thx i didn't need to add
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F) && (!doorIsClosed)
{
changeDoorState();
}
its work without it i only get errors when i use it.
do you have a solution for the door close automatically after 1h? for i have this solution but i don't get it to really work the door is closed after 5 sec but if i open and close the door then after 5 sec then the door close animation starts play once how to stop that if the door is already closed
script now :
#pragma strict
var theDoor : Transform;
private var drawGUI = false;
private var doorIsClosed = true;
function Update ()
{
if (drawGUI == true && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F))
{
changeDoorState();
}
}
function OnTriggerEnter (theCollider : Collider)
{
if (theCollider.tag == "Player")
{
drawGUI = true;
}
}
function OnTriggerExit (theCollider : Collider)
{
if (theCollider.tag == "Player")
{
drawGUI = false;
}
}
function OnGUI ()
{
if (drawGUI == true)
{
GUI.Box (Rect (Screen.width*0.5-51, 200, 102, 22), "Press F to open");
}
}
function changeDoorState ()
{
if (doorIsClosed)
{
theDoor.animation.CrossFade("Open");
//theDoor.audio.PlayOneShot();
doorIsClosed = false;
yield WaitForSeconds(5);
theDoor.animation.CrossFade("Close");
//theDoor.audio.Play();
doorIsClosed = true;
}
else
{
theDoor.animation.CrossFade("Close");
//theDoor.audio.Play();
doorIsClosed = true;
}
}