- Home /
Door animation after press a key
Hi Unity users. I got a problem, i'm new on Unity 3D and i'm trying to use an animation for my door. Well, first of all, i don't know how to make a start or what i need, but now my question is. If my player is walking to a door that i made and i press a key ( by example 'D' ). The door should open with an animation and it stays open. Can someone help me please. I was searching for it, or something like that. But i found nothing.
Please help someone. It's important.
Answer by Chris D · Jun 15, 2011 at 10:17 PM
There are a number of ways to accomplish what you're describing:
Actually have it animated (externally in a modelling program of some sort), then import
Do it by directly manipulating the transforms/rotations of the mesh
Do it with physics, using a hinge joint
To get it to trigger (if, say you wanted to walk up to it and press a 'use' key), you'd check if your player was within range, then check if the key is pressed.
Hope this helps!
Additionally, try following along with some tutorials on the Unity website or watchign a few on youtube - it really helps.
you said, "you'd check if your player was within range, then check if the key is pressed." How can i check that the player was within range. and how can i check the press key?
Again, there are a number of way you could go about doing that. If you're serious about making things in unity, try searching here and on google for some tutorials.
For now, you could:
Do a raycast from your character (assu$$anonymous$$g first person character)
Have a collider based around the door area and have an OnTriggerStay statement with an if (playerPressesTheDoorButton){ open the door }
You'll need the know-how to put that into action, though; so, again, tutorials/reading up on scripting.
i'm using this JS for my door.
function Update () { if (Input.Get$$anonymous$$eyDown ("d")) animation.Play ("DoorHor"); }
I created an animation for it and it works fine. When i press 'd', the door will open, but now i can open the door anywere in the scene. That's not the meaning of it. The door could only open when the player is standing close by the door and not anywere in the scene. How can i do this?
PS: And it need to play once. and not everytime when you hit the 'd' key. Because the animation is playing again when i hit the 'd' key.
Answer by fireDude67 · Jun 16, 2011 at 12:22 AM
I have seen the comment you put on the above answer by ChrisD, so I will answer those questions and provide some example code.
First, in order to make sure that the player is near the door, you must have a Box configured so that it will cover the area that is considered "close" in your game. Then, you must go into the BoxCollider, and set the "Is Trigger" option. This will make it so that it does not act like a wall. Next, you do not want the box to be visible. So, go back to the Box GameObject, and remove the MeshRender component.
Next, make a new script. I will use JavaScript, because that seems like what you are using. Unfortunately, I script mostly in C#, and have forgotten some of the syntax.
var DoorGameObject : Transform; // This is a reference to the door GameObject that has your animation
private var HasTriggerBeenUsed : boolean = false; // This is to make sure that the button is not repeatedly pressed.
private var setTrigger : boolean = false;
function OnTriggerStay() {
if (Input.GetKeyDown("d") && !HasTriggerBeenUsed) {
DoorGameObject.animation.Play("DoorHor");
setTrigger = true;
}
else if (Input.GetKeyDown("d") && HasTriggerBeenUsed) {
DoorGameObject.animation.Play("DoorClose"); // Insert name of DoorClose animation instead of that
setTrigger = true;
}
if (setTrigger) { HasTriggerBeenUsed = !HasTriggerBeenUsed; }
}
This script needs to be attached to the Box object that you created at the beginning. The variable, DoorGameObject
needs to be assigned to the object that has your door on it.
I think I covered everything, but if there is something I forgot, please leave a comment.
This is exactly were i was looking for, It works great!!!.
But i found a little foul in your JS. It is 'boolean' and not 'bool'. ;)
$$anonymous$$aybe i can ask you a little question? I got also an animation to close the door. Is it possible to close the door when i press "d"? Only if the door is opened, then it can work.
Edit: 19 juny 2011 - 00:14 A$$anonymous$$
Can you help me maybe with the last question, this is working, but i clicked wrong.
sure! i will update the script. unfortunately i was out of town for the past two days and could not see the comments.
It's not working :-( When i want to close the door, it plays the DoorHor again.
changed the script. apparently due to GameTime that would happen.
Not yet :(, But i think that it's enough. I changed my $$anonymous$$d. I will the door stays open, and your help was awesome. you help me a lot.
Your answer
Follow this Question
Related Questions
door opening 3 Answers
How to launch an animation pressing e 1 Answer
Press an in-game switch to open a door - help? 2 Answers
Teleporting script when i'm pressing a key in front of a door? 0 Answers