• 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 Blankets_McGee · Apr 20, 2017 at 02:09 AM · transform.position

Use WaitForSeconds instead of Input.KeyCode for automated doors

Here i have a simple door script. easy. not very optimized but works for now. I am trying to implement WaitForSeconds so that the doors open automatically instead of me having to press space. here is the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class openDoor : MonoBehaviour 
 {
     public GameObject doorLeft;
     public GameObject doorRight;
     public float doorLeftSpeed;
     public float doorRightSpeed;
     public float limitLeftDoor;
     public float limitRightDoor;
 
     private float limitLeftDoorCounter;
     private float limitRightDoorCounter;
 
     public bool openDoorsBool;
     public bool closeDoorsBool;
 
     // Use this for initialization
     void Start () 
     {
         //intentionally blank
     }
     
     // Update is called once per frame
     void Update () 
     {
         if (Input.GetKeyUp(KeyCode.Space))
         {
             if (openDoorsBool)
             {
                 openDoorsBool = false;
                 closeDoorsBool = true;
             }
             else
             {
                 openDoorsBool = true;
                 closeDoorsBool = false;
             }
         }
 
         if (openDoorsBool)
         {
             if (limitLeftDoorCounter >= limitLeftDoor)
             {
                 doorLeft.transform.position = new Vector3 (doorLeft.transform.position.x - doorLeftSpeed , doorLeft.transform.position.y, doorLeft.transform.position.z);
                 limitLeftDoorCounter -= doorLeftSpeed;
             }
 
             if (limitRightDoorCounter <= limitRightDoor)
             {
                 doorRight.transform.position = new Vector3 (doorRight.transform.position.x + doorRightSpeed , doorRight.transform.position.y, doorRight.transform.position.z);
                 limitRightDoorCounter += doorRightSpeed;
             }
         }
 
         if (closeDoorsBool)
         {
             if (limitLeftDoorCounter <= limitLeftDoor + 1)
             {
                 doorLeft.transform.position = new Vector3 (doorLeft.transform.position.x + doorLeftSpeed, doorLeft.transform.position.y, doorLeft.transform.position.z);
                 limitLeftDoorCounter += doorLeftSpeed;
             }
 
             if (limitRightDoorCounter >= limitRightDoor - 1)
             {
                 doorRight.transform.position = new Vector3 (doorRight.transform.position.x - doorRightSpeed, doorRight.transform.position.y, doorRight.transform.position.z);
                 limitRightDoorCounter -= doorRightSpeed;
             }
         }
     }
 }
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
0
Best Answer

Answer by Blankets_McGee · Apr 20, 2017 at 03:07 AM

Ok! I have worked through it and have a very "caveman" like approach to getting these doors to open. no animations involved just simply moving the transform position automatically. You can change the amount of seconds paused in the inspector. here is the updated code. probably could use some TLC .

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class openDoor : MonoBehaviour 
 {
     public GameObject doorLeft;
     public GameObject doorRight;
     public float doorLeftSpeed;
     public float doorRightSpeed;
     public float limitLeftDoor;
     public float limitRightDoor;
     public float waitForSeconds = 0;
 
     private float limitLeftDoorCounter;
     private float limitRightDoorCounter;
 
     public bool openDoorsBool;
     public bool closeDoorsBool;
 
     // Use this for initialization
     void Start () 
     {
         StartCoroutine (openDoorss ());
     }
 
     IEnumerator openDoorss()
     {
         yield return new WaitForSeconds (waitForSeconds);
 
             if (openDoorsBool)
             {
                 openDoorsBool = false;
                 closeDoorsBool = true;
             }
             else
             {
                 openDoorsBool = true;
                 closeDoorsBool = false;
             }
         
         if (openDoorsBool)
         {
             if (limitLeftDoorCounter >= limitLeftDoor)
             {
                 doorLeft.transform.position = new Vector3 (doorLeft.transform.position.x - doorLeftSpeed , doorLeft.transform.position.y, doorLeft.transform.position.z);
                 limitLeftDoorCounter -= doorLeftSpeed;
             }
 
             if (limitRightDoorCounter <= limitRightDoor)
             {
                 doorRight.transform.position = new Vector3 (doorRight.transform.position.x + doorRightSpeed , doorRight.transform.position.y, doorRight.transform.position.z);
                 limitRightDoorCounter += doorRightSpeed;
             }
         }
 
         if (closeDoorsBool)
         {
             if (limitLeftDoorCounter <= limitLeftDoor + 1)
             {
                 doorLeft.transform.position = new Vector3 (doorLeft.transform.position.x + doorLeftSpeed, doorLeft.transform.position.y, doorLeft.transform.position.z);
                 limitLeftDoorCounter += doorLeftSpeed;
             }
 
             if (limitRightDoorCounter >= limitRightDoor - 1)
             {
                 doorRight.transform.position = new Vector3 (doorRight.transform.position.x - doorRightSpeed, doorRight.transform.position.y, doorRight.transform.position.z);
                 limitRightDoorCounter -= doorRightSpeed;
             }
         }
     }
         
     // Update is called once per frame
     void Update () 
     {
         
     }
 }
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 Blankets_McGee · Apr 20, 2017 at 03:12 AM 0
Share

Sidenote: ...they don't close haha! working on the fix.

avatar image Blankets_McGee · Apr 20, 2017 at 03:47 AM 0
Share

Alrightyrooskie! So again, very simple caveman approach to opening doors you probably want to look into other sources. BUUUUUT we now have an opening and close set of elevator doors. simply drag your left door into the doorLeft gameobject space and etc. you can now set independent times for opening and closing the door with the openDoorSeconds and closeDoorSeconds fields. here is the updated code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class openDoor : $$anonymous$$onoBehaviour 
 {
     public GameObject doorLeft;
     public GameObject doorRight;
     public float doorLeftSpeed;
     public float doorRightSpeed;
     public float limitLeftDoor;
     public float limitRightDoor;
     public float openDoorsSeconds = 0;
     public float closeDoorsSeconds = 0;
 
     private float limitLeftDoorCounter;
     private float limitRightDoorCounter;
 
     public bool openDoorsBool;
     public bool closeDoorsBool;
 
     // Use this for initialization
     void Start () 
     {
         StartCoroutine (openDoorss ());
     }
 
     IEnumerator openDoorss()
     {
         yield return new WaitForSeconds (openDoorsSeconds);
 
         if (openDoorsBool) 
         {
             openDoorsBool = false;
             closeDoorsBool = true;
         } 
         else 
         {
             openDoorsBool = true;
             closeDoorsBool = false;
         }
         
         if (openDoorsBool) 
         {
             if (limitLeftDoorCounter >= limitLeftDoor) 
             {
                 doorLeft.transform.position = new Vector3 (doorLeft.transform.position.x - doorLeftSpeed, doorLeft.transform.position.y, doorLeft.transform.position.z);
                 limitLeftDoorCounter -= doorLeftSpeed;
             }
 
             if (limitRightDoorCounter <= limitRightDoor) 
             {
                 doorRight.transform.position = new Vector3 (doorRight.transform.position.x + doorRightSpeed, doorRight.transform.position.y, doorRight.transform.position.z);
                 limitRightDoorCounter += doorRightSpeed;
             }
         }
 
         yield return new WaitForSeconds (closeDoorsSeconds);
 
         if (closeDoorsBool) 
         {
             openDoorsBool = true;
             closeDoorsBool = false;
         }
         else 
         {
             openDoorsBool = false;
             closeDoorsBool = true;
         }
 
         if (closeDoorsBool)
         {
             if (limitLeftDoorCounter <= limitLeftDoor + 1)
             {
                 doorLeft.transform.position = new Vector3 (doorLeft.transform.position.x + doorLeftSpeed, doorLeft.transform.position.y, doorLeft.transform.position.z);
                 limitLeftDoorCounter += doorLeftSpeed;
             }
 
             if (limitRightDoorCounter >= limitRightDoor - 1)
             {
                 doorRight.transform.position = new Vector3 (doorRight.transform.position.x - doorRightSpeed, doorRight.transform.position.y, doorRight.transform.position.z);
                 limitRightDoorCounter -= doorRightSpeed;
             }
         }
     }
 
     // Update is called once per frame
     void Update () 
     {
         
     }
 }

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Ingame Object placing - Place objects like Unturned or Ark Survival 1 Answer

Having problems with setting transform.position 1 Answer

Setting a Global transform.position 0 Answers

Very weird. Transform position is at two places at the same time. 1 Answer

child object transform.position not working properly 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