user9609349
user9609349

Reputation: 91

Unity: how to make the update() function wait a few seconds

so Im trying to make my update() function wait a few seconds every time when the player presses the button "C" because every time when the player presses the key "c" it resets the rotation of the object and Im trying to make my game wait a few seconds because it will make some small animation of the object resets his rotation values.

void Reset()
{
    Vector3 newRotation = 
    gameObject.transform.rotation.eulerAngles;

    if (Input.GetKeyDown(KeyCode.C))
    {

        x = newRotation.x;
        y = newRotation.y;
        z = newRotation.z;

        x = Mathf.Round(x);
        y = Mathf.Round(y);
        z = Mathf.Round(z);

        yield return new WaitForSeconds(1);

        print(x + " " + y + " " + z);

        for (; x >= 0; x--)
        {

            arotation.x = x;
            boxy.transform.Rotate(arotation.x, y, z);
            if (x == 0)
            {
                for (; y >= 0; y--)
                {
                    arotation.y = y;
                    boxy.transform.Rotate(arotation.x, arotation.y, z);

                    if (y == 0)
                    {
                        for (; z >= 0; z--)
                        {
                            arotation.z = z;
                            boxy.transform.Rotate(arotation.x, arotation.y, arotation.z);
                        }
                    }
                }
            }
        }
        print(x + " " + y + " " + z);
    }
}

Upvotes: 0

Views: 11323

Answers (2)

FortCraftCoder
FortCraftCoder

Reputation: 43

Use this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class move : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine("Reset");
    }

    // Update is called once per frame
    void Update()
    {
    }
    IEnumerator Reset()
    {
        //Put your code before waiting here

        yield return new WaitForSeconds(1);

        //Put code after waiting here

        //You can put more yield return new WaitForSeconds(1); in one coroutine

        StartCoroutine("Reset");
    }
}

Upvotes: 0

mbilalbark
mbilalbark

Reputation: 94

You should add return type reset method. For example

IEnumerator Reset() {
   // your process
   yield return new WaitForSeconds(1);
  // continue process
} 

When you use this function you need to use the startcoroutine method.

void Update() {
     StartCoroutine("Reset");
}

Upvotes: 1

Related Questions