- Home /
Wait for seconds and then set a bool to true.
So I have ma de this script for my movement but i got the problem that IEnumerator time() won't run. I have tried to write it as. IEnumerator bool time() but is doesn't work either. Thanks for your help.
using UnityEngine; using System.Collections;
public class movement : MonoBehaviour {
//value for movement with keys
public float movspd = 10.0f;
public float trnspd = 100.0f;
//value for push movements
public float push = 10.0f;
public bool quickpush = true;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// This is the movement for the arrows
float move_Forward = movspd * Time.deltaTime * Input.GetAxis("Vertical");
float rotate = trnspd * Time.deltaTime * Input.GetAxis("Horizontal");
transform.Translate (Vector3.forward * move_Forward);
transform.Rotate (Vector3.up * rotate);
// Ends ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// This is the movement for the sida pushes
float push_p = push * Time.deltaTime;
float push_n = -push * Time.deltaTime;
if(Input.GetKey("a") && quickpush){
transform.Translate (Vector3.right * push_p);
quickpush = false;
time();
}
if(Input.GetKey("d") && quickpush){
transform.Translate (Vector3.right * push_n);
quickpush = false;
time();
}
// Ends ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if( Input.GetButton("reset")){
transform.position = new Vector3(0,0,0);
transform.eulerAngles = new Vector3(270,0,0);
}
}
IEnumerator time(){
yield return new WaitForSeconds(2);
quickpush = true;
return;
}
}
just use Invoke for simplicity, when you're learning
Answer by Xroft666 · Oct 27, 2012 at 10:57 AM
You should create a new Coroutine using function
StartCoroutine( time() )
ps: but think twice before creating a coroutine in update function. The code will create one coroutine per frame
You beat me to it.
Actually he has the quickpush set to false so it won't call it each frame.
Answer by fafase · Oct 27, 2012 at 11:01 AM
Try with:
StartCoroutine(time());
I would deeply recommend as well to avoid using name of objects that already exist. time is a variable from Time and could get you confused as which one you are using (despite the () for the function). Get used to using clear different names.
Your answer
Follow this Question
Related Questions
IEumerator not recognising when a bool has switched 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# Acessing animator on another Object 1 Answer
Make a laser flickering. 1 Answer