Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 sillyjake · Jul 09, 2015 at 09:09 AM · c#transformif

If statement not working correctly.

Hello, I've tried switching what it was checking, But when it does move, it keeps moving for ever. Help?

 using UnityEngine;
 using System.Collections;
 
 public class doorScript : MonoBehaviour {
 
     public GameObject top;
     public GameObject bottom;
     public bool open;
 
     // Use this for initialization
     void Start () {
         StartCoroutine (doorCheck ());
     }
     
     // Update is called once per frame
     IEnumerator doorCheck () {
         if (open) {
             if (top.transform.position.y < 3.49f) {
                 top.transform.Translate (Vector3.up * 0.005f);
             }
             if (bottom.transform.position.y > -3.6749f) {
                 bottom.transform.Translate (Vector3.down * 0.005f);
             }
         }
         yield return new WaitForSeconds (0.0125f);
         StartCoroutine (doorCheck ());
     }
 }
 
Comment
Add comment · Show 3
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 sillyjake · Jul 09, 2015 at 02:26 PM 0
Share

Because Update is too fast, I want the door to actually been seen sliding open.

avatar image Hellium · Jul 09, 2015 at 02:31 PM 0
Share

You just have to decrease the translation value then ...

avatar image sillyjake · Jul 09, 2015 at 02:37 PM 0
Share

But then, The top is stuck and the bottom goes forever

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Paul-Sinnett · Jul 09, 2015 at 09:22 AM

It works for me. It's an odd use for co-routines though. It looks like you're trying to re-implement Update:

     // Update is called once per frame
     void Update()
     {
         if (open)
         {
             if (top.transform.position.y < 3.49f)
             {
                 top.transform.Translate(Vector3.up * 0.005f);
             }
             if (bottom.transform.position.y > -3.6749f)
             {
                 bottom.transform.Translate(Vector3.down * 0.005f);
             }
         }
     }
 

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 Nischo · Jul 09, 2015 at 09:28 AM 0
Share

Remember that your code now is frame dependant. On a faster machine your code will open the door much faster. I guess thats what OP tried to avoid. But generally i aggree, using coroutines is a bit much here, but i blame the unity docs.

avatar image sillyjake · Jul 09, 2015 at 02:31 PM 0
Share

The top dosen't move and the bottom goes down forever.

avatar image Paul-Sinnett · Jul 14, 2015 at 01:53 PM 0
Share

Not for me. You don't have your bottom object upside down do you?

avatar image
0

Answer by Nischo · Jul 09, 2015 at 09:26 AM

Change the transform.Position to transform.localPosition and you might need to add Space.Self to your Translate calls. After that the code worked fine in my testbed.

You might want look into easing. Unity has a couple of really great plugins(iTween, HOTween, LeanTween etc.) that make this kind of animation easier to write and much nicer.

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
avatar image
0

Answer by barbe63 · Jul 10, 2015 at 01:58 AM

I think you should add an Animator component. But if you relly want to stick with code only, I'd say first you need to launch a Coroutine only when you need to open door instead of doing a perpetual test for nothing.

Then it's simple:

 public IEnumerator OpenDoor()
 {
     float speed = 1;
     //speed is a float to tweak to achieve the result you want

     while(top.transform.position.y < 3.49f)
     {
         top.transform.Translate (Vector3.up * Time.deltaTime * speed); 
         bottom.transform.Translate (Vector3.down * Time.deltaTime * speed);   
         yield return null;
     }
 }
 
 //Then you can call it from another script like you would for any public function, for example:
 
 StartCoroutine(doorScript.OpenDoor());


If your code still run for ever you should check for your 3.49f value to be right. Actually i'd rather use 2 Vector3 in the method that would be calculated in the beginning of the Coroutine to have the ended position like this:

  public IEnumerator OpenDoor()
  {
      float speed = 1;
      Vector3 topEndPosition=top.transform.position;
      topEndPosition.y+=3;
      Vector3 bottomEndPosition=bottom.transform.position;
      bottomEndPosition.y-=3;
      while(Vector3.Distance(top.transform.position, topEndPosition)>0.05f)
      {
          top.transform.position=Vector3.Lerp(top.transform.position, topEndPosition, Time.deltaTime * speed);
          bottom.transform.position=Vector3.Lerp(bottom.transform.position, bottomEndPosition, Time.deltaTime * speed);
          yield return null;
      }
  }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Referencing a script on the same object comes back with an error 4 Answers

how are Mathf.SmoothDamp and Mathf.SmoothStep different 1 Answer

How to get transform syncing over network to work? 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