- Home /
Need help with door opening and closing script/animation
So I have a problem I created an animation for my door and that went successfully and my script works just fine. But in my script it opens and closes the door all in one button push. How would I fix this?
Here's my code:
#pragma strict
var theDoor : Transform;
private var drawGUI = false;
private var doorIsClosed = true;
function Update ()
{
if (drawGUI == true && Input.GetKeyDown(KeyCode.E))
{
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, Screen.height*0.5, 102, 22), "E open");
}
}
function changeDoorState()
{
if (doorIsClosed == true)
{
theDoor.animation.CrossFade("Open");
doorIsClosed = false;
}
if (doorIsClosed == false)
{
theDoor.animation.CrossFade("Close");
doorIsClosed = true;
}
}
Answer by Pigifying · Feb 03, 2014 at 03:58 AM
Perhaps you could make this SUPER easy and do something like
var animate : AnimationClip function Update () if(Input.GetKeyDown("e")){ animation.clip = animate; animation.Play(); }
Attach this too your door with the animation attached and you should be good tell me if not.
Yeah it's still doing the same exact thing that would happen with my more confusing script
Answer by coldjalapeno · Feb 03, 2014 at 05:57 AM
,Try using GetKeyUp. I'm betting that since it's in Update() that you are actually getting several calls to changeDoorState() during the fraction of time that the key is held down. Throw a debug in there and check it out.
It works a little bit better but sometimes it skips the open animation and plays my doors close animation even when the door is already closed.
Try using an else statement in changeDoorState() if(true){do this and make false}else{do this and make true}
Now your problem is that once you open the door your bool is set to false then the next if statement is looking for that and acting on it.
Oh my god! IT WOR$$anonymous$$ED! thank you so much, I swore I did that before co$$anonymous$$g on here but I guess not XD either way thank you so much for the help!
Answer by Thunderturtle712 · Apr 23, 2014 at 01:55 AM
I am using raycasting to do this. Here is my script that you can use.
#pragma strict
private var guiShow : boolean = false;
private var isOpen : boolean = false;
var door : GameObject;
var rayLength = 10;
function Update ()
{
var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(transform.position, fwd, hit, rayLength))
{
if(hit.collider.gameObject.tag == "Door") //make sure to tag your door "Door" without the exclamtion points in the transform.
{
guiShow = true;
if(Input.GetKeyDown("e") && isOpen == false) // use any key you want to i chose e
{
door.animation.Play("DoorOpen"); //this is the opening animation that i used
isOpen = true;
guiShow = false;
}
else if(Input.GetKeyDown("e") && isOpen == true)
{
door.animation.Play("DoorClose"); //this is the closing animation i used
isOpen = false;
guiShow = false;
}
}
}
else
{
guiShow = false;
}
}
function onGUI ()
{
if(guiShow == true && isOpen == false)
{
GUI.Box(Rect(Screen.width / 2, Screen.height / 2, 100, 25), "Press e"); //this part is not needed
}
}
p.s when organizing your things dont put doors on your room level design. $$anonymous$$eep them by themselves Hierarcky because the doors flip out
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Make doors open with different keys? 1 Answer
Open door on input key 1 Answer
how to make a door opening and closing. 1 Answer
Why is my door opening and closing at the same time????????? 2 Answers