- Home /
I'd like to move a cube without delay.(C#)
Hello, guys.
(I'm not good at English well. Please understand me.)
Now I'm studying about moving a cube. I found one way to do this with StartCoroutine() and yield return new WaitForSeconds() codes. But I'd like to know there is another way to move a cube. The most important thing for me is 'No delay'. If there is a way to move a cube without delay(except count seconds), please teach me. Below is my code. Thanks!!
=======================================================
using UnityEngine;
using System.Collections;
public class Manager : MonoBehaviour {
public int state=0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
switch(state) // state = 4 -> wait
{
case 0:
StartCoroutine(forward());
break;
case 1:
StartCoroutine(backward());
break;
case 2:
StartCoroutine(left());
break;
case 3:
StartCoroutine(right());
break;
default:
break;
}
}
public IEnumerator forward()
{
state = 4;
iTween.MoveTo(GameObject.Find("Cube").gameObject, iTween.Hash( "y", 1, "easeType", iTween.EaseType.easeInOutSine));
yield return new WaitForSeconds(2.0f);
state = 1;
}
public IEnumerator backward()
{
state = 4;
iTween.MoveTo(GameObject.Find("Cube").gameObject, iTween.Hash( "y", -1, "easeType", iTween.EaseType.easeInOutSine));
yield return new WaitForSeconds(2.0f);
state = 2;
}
public IEnumerator left()
{
state = 4;
iTween.MoveTo(GameObject.Find("Cube").gameObject, iTween.Hash( "z", 1, "easeType", iTween.EaseType.easeInOutSine));
yield return new WaitForSeconds(2.0f);
state = 3;
}
public IEnumerator right()
{
state = 4;
iTween.MoveTo(GameObject.Find("Cube").gameObject, iTween.Hash( "z", -1, "easeType", iTween.EaseType.easeInOutSine));
yield return new WaitForSeconds(2.0f);
state = 0;
}
}
=======================================================
Just remove the yield return new WaitForSeconds ? --This causes it to pause for x seconds, which seems to NOT be what you want .
I've already tried your suggestion. But the cube doesnt move even one pixel.
And have you tried change the 2.0f to something much smaller like 0.01 ? It would be virtually impossible to distinguish a 0.001 second pause.
There really isn't too much I can say since I have never used iTween before . . . But perhaps I can write a script that does not use iTween?
yea, you should probably write a script that doesn't use itween
Have you tried changing those waits to
yield return null
?
Your answer
![](https://koobas.hobune.stream/wayback/20220613084407im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
C# WaitForSeconds doesn't seem to work ?? 2 Answers
delayed function problem 1 Answer
Make delay for spawn 3 Answers
Time Delay isnt working 1 Answer
How to Delay A Ball shooting script 1 Answer