WaitForSeconds won't wait (Corotine),WaitForSeconds not waiting
So the goal of this script is to randomly move an object around the screen, changing direction when hitting a wall. I want it to randomly stop moving for a short period of time, and to do that I'm trying to use the WaitForSecondsRealtime corotine function. But for some reason that I cannot figure out, the corotine runs fine but just does not wait and just seems to continue the script as if the WaitForSeconds function doesn't exist. I'm not very good at coding (I'd consider myself a beginner), and a lot of the coding in my script I found online, but I think I understand it well enough to edit it. WaitForSeconds seems to have the same problem as WaitForSecondsRealtime. I've been staring at this script for days and I've completely confused myself. I've also looked at every similar problem I could find on the internet, but I think I did everything they did and it still won't pause the script. Can anyone help?
using UnityEngine; using System.Collections;
public class randomMovement : MonoBehaviour { public float moveSpeed; public Vector3 dir; public float turnSpeed; float targetAngle; Vector3 currentPos; bool play = true; Vector3 direction; float stop = 0;
void Start()
{
dir = Vector3.up;
InvokeRepeating("Start1", 0f, 5f);
}
void Start1()
{
play = true;
direction = new Vector3(Random.Range(-3.0f, 3.0f), Random.Range(-4.0f, 4.0f), 0); //random position in x and y
}
void Update()
{
currentPos = transform.position;//current position of gameObject
if (play)
{ //calculating direction
dir = direction - currentPos;
dir.z = 0;
dir.Normalize();
play = false;
}
if (stop >= 7) //Should occur ~ 30% of the time?
{
Debug.Log("stop >= 7");
moveSpeed = 1;
Vector3 target = dir * moveSpeed + currentPos; //calculating target position
transform.position = Vector3.Lerp(currentPos, target, Time.deltaTime);//movement from current position to target position
targetAngle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - 90; //angle of rotation of gameobject
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 0, targetAngle), turnSpeed * Time.deltaTime); //rotation from current direction to target direction
}
if (stop < 7) //Stops movement and starts corotine
{
Debug.Log("stop < 7");
stop = Random.Range(0, 10);
Corotine();
}
Debug.Log(stop);
}
void Corotine()
{
StartCoroutine(PauseMovement());
}
IEnumerator PauseMovement() //Pauses movement while drawing a new stop value for next rendition
{
stop = Random.Range(0, 10);
yield return new WaitForSecondsRealtime(Random.Range(1,5));
}
void OnCollisionEnter2D()
{
CancelInvoke();//stop call to start1 method
direction = new Vector3(Random.Range(-3.0f, 3.0f), Random.Range(-4.0f, 4.0f), 0); //again provide random position in x and y
play = true;
}
void OnCollisionExit2D()
{
InvokeRepeating("Start1", 2f, 5f);
}
}
Your answer
Follow this Question
Related Questions
Update() and FixedUpdate() not running on C# scripts 0 Answers
Assigning a PreFab to a pre existing variable. 0 Answers
Creating Splines from empties in script 0 Answers
Why is the code below not setting the value for the timer? 0 Answers
Multiple GameObjects with the same Script are sharing the same bool values when they shouldn't. 1 Answer