Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 sashathemouse · Mar 12, 2013 at 08:34 PM · noobdoor

Open door on input key

I'm trying to let the character be able to open a door on mousing over it and pressing e. I have tried a script which rotates and moves the door which successfully opens it but on clicking e a second time the door moves forward from the doorway.

   var door  : GameObject; 
   private var x : float;
   x = 2.6;
   private var y : float;
   y = 1.4;
   private var rot : int;
   rot = 240;
   
 private var mouse : boolean = false;
 
 function OnMouseOver ()
     {
     mouse = true;
     }
     
 function OnMouseExit ()
     {
     mouse = false;
     }
 function Update () {
 
     if(Input.GetKeyDown("e") && mouse == true)
     {
     door.transform.Translate(x,y,0);
     door.transform.Rotate(0,0,rot);
 
     rot = -1 * rot;
     }
 }

I have also tried using two door and open and a closed one and simply translating them. The open door moves into place on clicking e and the closed door moves out of sight but the process is not reversing properly

  var y : float;
 
                 y = 5;
 
 private var mouse: boolean;
 
                 mouse = false;
 
 var initial : GameObject;
 
 var attached : GameObject;
 
  
 
  
 
 function OnMouseEnter
 
                 {
 
 mouse = true;
 
                 }
 
  
 
 function OnMouseExit
 
                 {
 
 mouse = false;
 
                 }
 
  
 
 function Update
 
                 {
 
 if (mouse = true && input.GetKeyDown (ā€œeā€))
 
                 {
 
 initial.Tranform.translate (0,y,0);
 
 attached.Transform.translate (0,y,0);
 
  
 
 y = y * -1;

thanks for the help

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

1 Reply

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

Answer by LightSource · Mar 12, 2013 at 08:42 PM

Try this: Just attach the animation for the door opening...

 var ray : Ray = camera.main.ScreenPointToRay (Input.mousePosition);
 var hit : RaycastHit;
 
 if (Physics.Raycast (ray.origin, ray.direction, hit, 100)) 
 
 {
     if(hit.collider.tag == "yourObject") && (Input.GetKeyDown ("e"))
     
 {
 
 animation.Play("yourAnimation");
 
 }
Comment
Add comment · Show 5 · 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 sashathemouse · Mar 12, 2013 at 10:53 PM 0
Share

hey thanks for the advice... but im new to unity what should i use to make an animation.

avatar image sashathemouse · Mar 12, 2013 at 10:54 PM 0
Share

Can one be made with in unity or will i need to use a program like blender.

avatar image LightSource · Mar 12, 2013 at 11:55 PM 0
Share

Take a look at this video for beginners in animation. I also just explained a step by step process in the second link.

Video: http://www.youtube.com/watch?v=HrTlFSztcOw

Second Link: http://answers.unity3d.com/questions/414940/can-i-scale-an-animation-with-a-model.html

avatar image sashathemouse · Mar 22, 2013 at 10:43 PM 0
Share

Thanks I have been able to get the door to open with an animation but can I make the door close if the player hits e on an open door

I tried this code to no avail...

 var attached : GameObject;
 private var state : int = 1;
 private var mouse : boolean = false;
 
 function On$$anonymous$$ouseEnter ()
     {
     mouse = true;
     }
 function On$$anonymous$$ouseExit ()
     {
     mouse = false;
     }
 
 function Update ()
     {
     if (Input.Get$$anonymous$$eyDown("e") && state == 1 && mouse == true)
         {
         animation.Play("Open");
         state = state * -1;
         }
     if (Input.Get$$anonymous$$eyDown("e") && state == -1 && mouse == true)
         {
         animation.Play("Shut"); 
         state = state * -1;
         }
     }
         
     
avatar image LightSource · Mar 22, 2013 at 11:00 PM 0
Share

You need to detect if the door is in front of you. Use that raycast component in my previous script

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Need help with door opening and closing script/animation 3 Answers

Targetting error please help 0 Answers

How to get one script to manage many sprites 0 Answers

Help with the update function and rigidbodys 3 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