- Home /
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);
}
}
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");
}
}
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;
}
}
}
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.
Your answer
Follow this Question
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