• 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 EnzoKarate · Feb 10, 2013 at 04:32 AM · javascriptdoor

How Do I make my door always open when I press the key?

I created a javascript to make a open-able door. It works, well, usually. Sometimes when i press the key to open the door, it opens a little, then closes again. Could someone help me? Here is the code:

pragma strict

  var Smooth : float = 1.0;
  var OpenAngle : float = 90.0;
  var CloseAngle : float = 0.0;
  var OpenDoor : boolean = false;
  var EnterTrigger : boolean = false;
  var DoorIsOpen : boolean = false;
  var DoorOpenSound : AudioClip;
 
 function OnTriggerEnter (Col: Collider){
     if(Col.gameObject.tag == "Player"){
         EnterTrigger = true;
     }
 }

 function OnTriggerExit (Col: Collider){
     if(Col.gameObject.tag == "Player"){
         EnterTrigger = false;
     }
 }

 function OpenOrCloseDoor(){
     if(DoorIsOpen == false){
                 OpenDoor = true;
                 audio.PlayOneShot(DoorOpenSound);
             }
             else if(DoorIsOpen == true){
                 OpenDoor = false;
                 audio.PlayOneShot(DoorOpenSound);
             }
 }

 //Every Frame Do  
 function Update () {
      //Open Door Part Starts     
     if (OpenDoor == true) {
         var DoorOpen = Quaternion.Euler(0, OpenAngle, 0);
         transform.localRotation = Quaternion.Slerp(transform.localRotation, DoorOpen, Time.deltaTime * Smooth);
         DoorIsOpen = true;
     }
     if (OpenDoor == false) {
         var DoorClosed = Quaternion.Euler(0, CloseAngle, 0);
         transform.localRotation = Quaternion.Slerp(transform.localRotation, DoorClosed, Time.deltaTime * Smooth);
         DoorIsOpen = false;
     }
     //Open Door part Ends, trigger part Starts.
     if( EnterTrigger == true){
         if(Input.GetKey(KeyCode.E)){
             OpenOrCloseDoor();
         }
     }
 }

//End of Update

Thanks a lot.

Comment
Add comment · Show 1
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 cdrandin · Feb 10, 2013 at 04:44 AM 0
Share

Assu$$anonymous$$g DoorIsOpen = false and EnterTrigger = true. Then press 'E' Cal OpenOrCloseDoor()... So OpenDoor = true then when it reaches the next frame in the update function DoorIsOpen = false so the door is open for 1 frame. If you did it procedural it would be easier for you ins$$anonymous$$d of combine 2 functionalities into 1. So, have one that focuses on when the door should open then another function focusing on when the door should close.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Feb 10, 2013 at 05:11 AM

You should not use Input.GetKey: this toggles DoorIsOpen every frame while the key is pressed, and the actual door state when the key is released is unpredictable - use Input.GetKeyDown instead. Additionally, ou should not modify DoorIsOpen when opening/closing the door - by the way, DoorIsOpen and OpenDoor are redundant: keep one or another.
The whole script could be highly simplified, like below:

 var Smooth : float = 1.0;
 var OpenAngle : float = 90.0;
 var CloseAngle : float = 0.0;
 var OpenDoor : boolean = false;
 var EnterTrigger : boolean = false;
 var DoorOpenSound : AudioClip;
 private var curAngle: float = 0;
  
 function OnTriggerEnter (Col: Collider){
     if(Col.gameObject.tag == "Player"){
         EnterTrigger = true;
     }
 }
  
 function OnTriggerExit (Col: Collider){
     if(Col.gameObject.tag == "Player"){
         EnterTrigger = false;
     }
 }
 
 function Update () {
     // when E is pressed...
     if (EnterTrigger && Input.GetKeyDown(KeyCode.E)){
         DoorIsOpen = !DoorIsOpen; // toggle DoorIsOpen...
         audio.PlayOneShot(DoorOpenSound); // and start the door sound
     }
     var angle: float; // define the target angle:
     if (OpenDoor){
         angle = OpenAngle;
     } else {
         angle = CloseAngle;
     }
     // "rotate" curAngle smoothly to the target angle
     curAngle = Mathf.MoveTowards(curAngle, angle, Time.deltaTime * Smooth);
     // update actual door rotation
     transform.localEulerAngles = Vector3(0, curAngle, 0);
 }
Comment
Add comment · Show 2 · 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 EnzoKarate · Feb 13, 2013 at 05:44 AM 0
Share

Thanks a lot, i am still starting to learn how to program using JavaScript and, well , I'm younger then 15. So thanks for the help. Do you $$anonymous$$d if i use the above code? thx

avatar image aldonaletto · Feb 13, 2013 at 01:23 PM 0
Share

No trouble - use it for any purpose!

avatar image
0

Answer by segahogzombie · Aug 04, 2015 at 08:21 PM

using UnityEngine; using System.Collections;

public class SmallDoorExample : MonoBehaviour {

 public float closedAngle = 0;
 public float openedAngle = 90;
 public float doorSwingSmoothingTime = 0.5f;
 public float doorSwingMaxSpeed = 90;
 
 private float targetAngle;
 private float currentAngle;
 private float currentAngularVelocity;
 
 void Update () 
 {
     if (DoorWasInteractedWith ())
         ToggleAngle ();
     
     UpdateAngle ();
     UpdateRotation ();
 }
 
 static bool DoorWasInteractedWith ()
 {
     return Input.GetKeyDown (KeyCode.E);
 }
 
 void ToggleAngle ()
 {
     if (targetAngle == openedAngle)
         targetAngle = closedAngle;
     else
         targetAngle = openedAngle;
 }
 
 void UpdateAngle ()
 {
     currentAngle = Mathf.SmoothDamp (currentAngle, 
                                      targetAngle, 
                                      ref currentAngularVelocity, 
                                      doorSwingSmoothingTime, 
                                      doorSwingMaxSpeed);
 }
 
 void UpdateRotation ()
 {
     transform.localRotation = Quaternion.AngleAxis (currentAngle, Vector3.up);
 }

}

Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

[Closed]Script that opens doors with the press of a key. 0 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Door key scripting problem 0 Answers

Opening an animated door with a keycard 0 Answers

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


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