I’m trying to create a script to make a cube move randomly around the screen, but it’s just randomly moving super fast, so there’s no way to click on it. How would I slow this down?
function Update ()
{
LerpCube();
}
function LerpCube()
{
var randomPosition = Vector3 (Random.Range( 6,-6 ), Random.Range( 4, -4 ));
transform.position = Vector3.Lerp (transform.position, randomPosition, 1);
}
you could try usingyield WaitForSeconds (1); Inside the LerpCube function but i dont know how that would act with you calling LerpCube() through the update function i’ve never tried it.
like i said since your calling the lerpcube function through the update function your lerpcube function is called hundreds/thousand times a second so the yield never gets a chance to do its job before the function is called again.
Try this it makes sure the update doesnt interfere with the yield
Thanks! That worked, but how would I make the movement smooth? Right now it’s just flashing to different spots. I thought lerp made the movement smooth?
Ok so i messed about with it and messed some more and i have a solution the Time.deltaTime was in the wrong place so i moved it but it needs to constantly update so that it can moove smoothly so i moved the object movement to the update changed it to FixedUpdate to make the movement smoother but you can use Update too if you want. but the randomPosition variable needs to update once every second or so do i added a variable that you can change moveDelay.
I had to declare randomPosition at the top so that it could be referenced in the update.
The random position only updates in the LerpCube function now
here it is
public var waiting : boolean = false;
var randomPosition : Vector3;
var moveDelay : float = 1;
function FixedUpdate ()
{
if (waiting == false)
{
SendMessage("LerpCube");
SendMessage("Wait");
}
else
{
transform.position = Vector3.Lerp (transform.position, randomPosition, Time.deltaTime*1);
}
}
function LerpCube()
{
randomPosition = Vector3 (Random.Range( 6,-6 ), Random.Range( 4, -4 ));
}
function Wait()
{
waiting = true;
yield WaitForSeconds (moveDelay);
waiting = false;
//Debug.Log ("waited");
}
Ill see if i can clean this up a bit maybe get rid of the Wait function in a bit
but this definitely works
Edit:
this is cleaner
public var waiting : boolean = false;
var randomPosition : Vector3;
var moveDelay : float = 1;
function FixedUpdate ()
{
if (waiting == false)
{
SendMessage("LerpCube");
}
else
{
transform.position = Vector3.Lerp (transform.position, randomPosition, Time.deltaTime*1);
}
}
function LerpCube()
{
randomPosition = Vector3 (Random.Range( 6,-6 ), Random.Range( 4, -4 ));
waiting = true;
yield WaitForSeconds (moveDelay);
waiting = false;
//Debug.Log ("waited");
}
This works brilliantly, but I have a couple of questions about it. I notice that it works even if the var moveDelay is set to 0. Why is that? Also, since it’s using WaitForSeconds, does that mean it would not be able to receive other input (eg, OnTriggerEnter) while it is waiting?