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 liquifiednate · Feb 03, 2014 at 03:24 AM · dooropenclose

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;
 
 }
 
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · 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 liquifiednate · Feb 03, 2014 at 05:06 AM 0
Share

Yeah it's still doing the same exact thing that would happen with my more confusing script

avatar image
0

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.

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 liquifiednate · Feb 03, 2014 at 06:44 AM 0
Share

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.

avatar image coldjalapeno · Feb 03, 2014 at 06:59 AM 0
Share

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.

avatar image liquifiednate · Feb 03, 2014 at 08:02 AM 0
Share

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!

avatar image
0

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
     }
 }
Comment
Add comment · Show 1 · 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 Thunderturtle712 · Apr 23, 2014 at 02:02 AM 0
Share

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

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

21 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 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

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


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