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 CalvinAMi · May 29, 2015 at 07:14 AM · c#lerpwaitforsecondslocalscaleelse if

How to use WaitForSeconds inside IF / Else If statement?

Hey gang,

I am having a problem creating a pause in between two positions of a single gameObject.

I put a Cube in my scene. I then created a duplicate of the Cube and left it in the same position, but I hid the Box Collider and Mesh Renderer.

Next, I created a second duplicate of the Cube and did the same. This time I moved the Cube a small distance away.

I created a C# script that successfully moves/rotates/scales the Cube from position1 to position2.

Now I want to insert a 3 second pause after the Cube reaches each position.

I have attempted using WaitForSeconds MANY different ways, but can't seem to get it to work when I am using IF / ELSE IF or ChangeTarget.

Any tips?

here's my code:

 using UnityEngine;
 using System.Collections;
 
 public class MoveCube : MonoBehaviour {
 
     public Transform moveCube;
     public Transform position1;
     public Transform position2;
     public Vector3 newPosition;
     public Quaternion newRotation;
     public Vector3 newScale;
     public string currentState;
     public float lerpMove;
     public float resetTime;
     
     void Start () {
         ChangeTarget ();
     }
     
     void FixedUpdate () {
         moveCube.position = Vector3.Lerp (moveCube.position, newPosition, lerpMove * Time.deltaTime);
         moveCube.rotation = Quaternion.Lerp (moveCube.rotation, newRotation, lerpMove * Time.deltaTime);
         moveCube.localScale = Vector3.Lerp (moveCube.localScale, newScale, lerpMove * Time.deltaTime);
     }
     
     void ChangeTarget (){
         
         //if we are at position1, then move to position2
         if (currentState == "Moving to position 1") {
             currentState = "Moving to position 2";
             newPosition = position2.position;
             newRotation = position2.rotation;
             newScale = position2.localScale;
         }
 
         //ADD 3 SECOND PAUSE HERE
 
         //if we are at position2, then move to position1
         else if (currentState == "Moving to position 2") {
             currentState = "Moving to position 1";
             newPosition = position1.position;
             newRotation = position1.rotation;
             newScale = position1.localScale;
         }
 
         //ADD 3 SECOND PAUSE HERE
 
         //the game first starts from the default state of the gameObject and moves to position2
         else if (currentState == "") {
             currentState = "Moving to position 2";
             newPosition = position2.position;
             newRotation = position2.rotation;
             newScale = position2.localScale;
         }
         Invoke ("ChangeTarget", resetTime);
     }
 }
Comment
Add comment · Show 2
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 Mehul-Rughani · May 29, 2015 at 07:51 AM 1
Share

if you want to use waitforseconds then you have to use Coroutine insted of method...

for example

 using UnityEngine;
 using System.Collections;
 
 public class Test : $$anonymous$$onoBehaviour {
 
     // Use this for initialization
     void Start () {
         StartCoroutine(SetImageAlpha());
     }
 
     IEnumerator SetImageAlpha()
     {
         Debug.Log("Execute When It Call");
         yield return new WaitForSeconds(2);
         Debug.Log("Execute after 2 sec When It Call");
     }
 
 }
avatar image CalvinAMi · Jun 04, 2015 at 10:47 PM 0
Share

Okay =( I'm still not getting it. I've re-written my $$anonymous$$ovingPlatform script as follows.

It successfully gives the Debug info that it waited, but it didn't actually pause the script.

What am I doing wrong and how do I fix this?

ALSO... my script allows the user to input the number of Transform's it will move between with this line: "public Transform[] Waypoints;". How can move the "delayTimer" so each added transform will have its own delay timer? That way, I can make the moving platform stop at specified waypoints and have a delay of 0 at the waypoints I don't want to have a delay.

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$ovingPlatform : $$anonymous$$onoBehaviour 
 {
     public Transform[] Waypoints;
     public float moveSpeed = 3;
     public float rotateSpeed = 0.5f;
     public float scaleSpeed = 0.5f;
     public float delayTimer = 3;
     public int CurrentPoint = 0;
     
 
     IEnumerator timeToDelay()
     {
         Debug.Log("Delay timer activated...");
         yield return new WaitForSeconds(delayTimer);
         Debug.Log("Delay timer completed!");
     }
 
     void FixedUpdate () 
     {
         if (transform.position != Waypoints [CurrentPoint].transform.position) 
         {
             transform.position = Vector3.$$anonymous$$oveTowards (transform.position, Waypoints [CurrentPoint].transform.position, moveSpeed * Time.deltaTime);
             transform.rotation = Quaternion.Lerp (transform.rotation, Waypoints [CurrentPoint].transform.rotation, rotateSpeed * Time.deltaTime);
             transform.localScale = Vector3.Lerp (transform.localScale, Waypoints [CurrentPoint].transform.localScale, scaleSpeed * Time.deltaTime);
         }
         
         if (transform.position == Waypoints [CurrentPoint].transform.position) 
         {
             CurrentPoint += 1;
             StartCoroutine("timeToDelay");
         }
 
         if( CurrentPoint >= Waypoints.Length)
         {
             CurrentPoint = 0;
         }
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by nesis · May 29, 2015 at 07:52 AM

WaitForSeconds is a special class. It's used as a coroutine's yield object. You can't use it in any function: the function must be a coroutine. Coroutine functions have a return type of IEnumerator.

For example:

 //moves this object to the origin, waits for 2 seconds, then moves it to (10,10,10)
 IEnumerator MyCoroutine(float someParameter)
 {
     transform.position = Vector3.zero;
     yield return new WaitForSeconds(2f);
     transform.position = new Vector3(10f,10f,10f);

 }

Note that coroutines require that you have at least one yield statement in them. If you're wanting to work around that while testing, just add a yield break to the very end of your method. It's a special yield object that causes the coroutine to exit early.

To start a coroutine, you need to call StartCoroutine(). For example, StartCoroutine(MyCoroutine(5f)); will start the above coroutine.

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to Scale an object up and down over time? 5 Answers

c# wait script 2 Answers

Help using Lerp inside of a coroutine. 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