Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by NomDeiX · Mar 28, 2017 at 06:32 PM · c#keypresskeyframepressed

Door Script Problem

Hello there, I need help due to my door script problems. Opening doors with E key button works definitely fine except first try. That means that when i launch game mode, go to the collider and press E nothing opens (but audio is played). You have to double tap the button to open doors for first time, but after that everything works normal (one tap opening door). Any suggestions ? Script : {

     public bool open;
     public bool close;
     public bool inTrigger;
     public Transform doorHinge;
     public AudioSource audio;
 
 
     void Start ()
     {
 
 
 
     }
 
 
     void OnTriggerEnter(Collider other)
     {
         inTrigger = true;
     }
 
     void OnTriggerExit(Collider other)
     {
         inTrigger = false;
     }
 
     void Update()
     {
         if (inTrigger)
         {
             if (close)
             {
                 
 
                     if (Input.GetKeyDown(KeyCode.E))
                     {
                         open = true;
                         close = false;
                         audio.Play ();
                     
                 }
             }
 
             else
             {
                 if (Input.GetKeyDown(KeyCode.E))
                 {
                     close = true;
                     open = false;
                     audio.Play ();
                 }
             }
         }
 
         if (open)
         {
             var newRot = Quaternion.RotateTowards(doorHinge.rotation, Quaternion.Euler(0.0f, 90.0f, 0.0f), Time.deltaTime * 250);
             doorHinge.rotation = newRot;
 
         }
         else
         {
             var newRot = Quaternion.RotateTowards(doorHinge.rotation, Quaternion.Euler(0.0f, 0.0f, 0.0f), Time.deltaTime * 250);
             doorHinge.rotation = newRot;
         }
     }
 
     void OnGUI()
     {
         if (inTrigger)
         {
             if (open)
             {
                 GUI.Box(new Rect(0, 0, 400, 50), "Press E to close");
             }
             else
             {
                 
 
                     GUI.Box(new Rect(0, 0, 400, 50), "Press E to open");
 
 
             }
         }
     }
 }
 
Comment
Add comment · Show 4
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 Positive7 · Mar 28, 2017 at 10:31 PM 0
Share
 private void Start()
 {
     this.open = this.doorHinge.rotation == Quaternion.Euler(0.0f, 90.0f, 0.0f);
     this.close = this.doorHinge.rotation == Quaternion.identity;
 }
avatar image NomDeiX Positive7 · Mar 29, 2017 at 05:26 PM 0
Share

Doesnt work

avatar image Positive7 NomDeiX · Mar 29, 2017 at 05:41 PM 1
Share
 using UnityEngine;
 
 public class DoorTest : $$anonymous$$onoBehaviour
 {
     public bool open;
 
     public bool close;
 
     public bool inTrigger;
 
     public Transform doorHinge;
 
     public AudioSource audioSource;
 
     private readonly Quaternion openRot = Quaternion.Euler(0.0f, 90.0f, 0.0f);
     private readonly Quaternion closeRot = Quaternion.Euler(0.0f, 0.0f, 0.0f);
 
     private void Start()
     {
         this.open = this.doorHinge.rotation.y >= 90.0f;
 
         this.close = this.doorHinge.rotation.y >= 0.0f;
     }
 
     private void OnTriggerEnter(Collider other)
     {
         this.inTrigger = true;
     }
 
     private void OnTriggerExit(Collider other)
     {
         this.inTrigger = false;
     }
 
     private bool Set$$anonymous$$eOnly()
     {
         this.open = this.close = false;
         return true;
     }
 
     private void Update()
     {
         if (this.inTrigger)
             if (this.close && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
             {
                 this.open = this.Set$$anonymous$$eOnly();
                 this.audioSource.Play();
             }
             else if (this.open && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
             {
                 this.close = this.Set$$anonymous$$eOnly();
                 this.audioSource.Play();
             }
 
         this.doorHinge.rotation = Quaternion.RotateTowards(this.doorHinge.rotation, this.open ? this.openRot : this.closeRot, Time.deltaTime * 250);
     }
 
     private void OnGUI()
     {
         if (!this.inTrigger) return;
         GUI.Box(new Rect(0, 0, 400, 50), this.open ? "Press E to close" : "Press E to open");
     }
 }
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Rhombuster · Mar 29, 2017 at 07:25 PM

It may have to do with the value of what open and close start at. Neither is instantiated and I can't remember if they are defaulted to true or false. Try setting one to true, and one to false before the game runs. So either at the top of the class or like this:

      private void Start()
      {
          open = true;
          close = false;
      }

It depends on the state of your door at the start, so if it starts closed, set close to true and open to false.

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.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

Play animation once (when down key pressed) in c# 1 Answer

Checking if any key is up? 3 Answers

How to detect a keyboard key held down? 2 Answers

Why does this condition keep being true even if its not? 0 Answers

How to instantiate a game object every time when space key is pressed 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