Script creates a loop of the door opening and closing
Does anybody have a solution? I'm trying to get the script to check if the door is open, if so pressing E will close it and vice versa.
if (Input.GetKey (KeyCode.E) && (gateOpen == false)) //check for key press
{
print ("e was pressed to open");
gateControl ("open");
gateOpen = true;
}
if (Input.GetKey (KeyCode.E) && (gateOpen == true)) //check for key press2
{
print ("e was pressed to close");
gateControl ("close");
gateOpen = false;
Answer by Dave-Carlile · Sep 08, 2015 at 09:11 PM
You probably want to use Input.GetKeyDown instead of Input.GetKey
. GetKey returns true on every frame as long as the key is held down, so your code will very quickly toggle back and forth between open and closed. GetKeyDown returns true only on the frame that the key was pressed, and won't return true again until the key is released then pressed again. So this will give you a single press and your code will only toggle the door state once.
Thanks for that @Dave Carlile! That actually solved another one of my problems. $$anonymous$$y other issue is that this script opens the door on e press/looking at door, then tells itself that the doors open, but at the same time it recognizes that its open and tries to close it...if that makes sense. I'm just trying to sort out some logic.
Answer by MustacheBill · Sep 09, 2015 at 03:38 AM
solved! it was an issue in my animation controller, the script left the opened and closed state as true so it created a loop.
Your answer
Follow this Question
Related Questions
Something interesting with code 0 Answers
Raycasting Animations 0 Answers
Why is my door not working properly? 0 Answers
Opening/Closing doors 1 Answer
CS1519 error 0 Answers