Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ilias · Jun 15, 2011 at 10:09 PM · animationdoorkey

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Chris D · Jun 15, 2011 at 10:17 PM

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ilias · Jun 15, 2011 at 10:29 PM 0
Share

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?

avatar image Chris D · Jun 15, 2011 at 10:45 PM 0
Share

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 (assu$$anonymous$$g 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.

avatar image Ilias · Jun 15, 2011 at 11:41 PM 0
Share

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.

avatar image
2
Best Answer

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.

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ilias · Jun 16, 2011 at 10:38 PM 0
Share

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.

avatar image fireDude67 · Jun 20, 2011 at 03:59 AM 0
Share

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

avatar image Ilias · Jun 22, 2011 at 09:47 PM 0
Share

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

avatar image fireDude67 · Jun 23, 2011 at 02:52 AM 0
Share

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

avatar image Ilias · Jun 23, 2011 at 10:22 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to set-up a Door that locks behind you after walk in to a room, but need a iPhone as a key to get out? 1 Answer

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges