x


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.

more ▼

asked Jun 15 '11 at 10:09 PM

Ilias gravatar image

Ilias
38 20 22 28

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

There are a number of ways to accomplish what you're describing:

  1. Actually have it animated (externally in a modelling program of some sort), then import
  2. Do it by directly manipulating the transforms/rotations of the mesh
  3. 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.

more ▼

answered Jun 15 '11 at 10:17 PM

Chris D gravatar image

Chris D
2.5k 5 7 25

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?

Jun 15 '11 at 10:29 PM Ilias

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:

  1. Do a raycast from your character (assuming first person character)
  2. 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.

Jun 15 '11 at 10:45 PM Chris D

i'm using this JS for my door.

function Update () { if (Input.GetKeyDown ("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.

Jun 15 '11 at 11:41 PM Ilias
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jun 16 '11 at 12:22 AM

fireDude67 gravatar image

fireDude67
945 9 15 30

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'. ;)

Maybe 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 AM

  • Can you help me maybe with the last question, this is working, but i clicked wrong.
Jun 16 '11 at 10:38 PM Ilias

sure! i will update the script. unfortunately i was out of town for the past two days and could not see the comments.

Jun 20 '11 at 03:59 AM fireDude67

It's not working :-( When i want to close the door, it plays the DoorHor again.

Jun 22 '11 at 09:47 PM Ilias

changed the script. apparently due to GameTime that would happen.

Jun 23 '11 at 02:52 AM fireDude67

Not yet :(, But i think that it's enough. I changed my mind. I will the door stays open, and your help was awesome. you help me a lot.

Jun 23 '11 at 10:22 PM Ilias
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3807
x194
x166

asked: Jun 15 '11 at 10:09 PM

Seen: 3007 times

Last Updated: Nov 12 '12 at 09:50 PM