Moving Object around screen

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.

I tried that, but it didn’t really help.

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 :smile:

let me know if it works

var waiting : boolean = false;

function Update () 
{
	if (waiting == false)
	{
		LerpCube();
	}
		else
		{
			return;
		}
}


function LerpCube()
{
	var randomPosition = Vector3 (Random.Range( 6,-6 ), Random.Range( 4, -4 ));
	transform.position = Vector3.Lerp (transform.position, randomPosition, 1);
	waiting = true;
	yield WaitForSeconds (1);
	waiting = false;
}

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?

Multiply whatever you are moving by Time.deltaTime.

That seems to make the movements smaller, but it is still as jerky as before.

I’m not incredibly familiar with lerps but i think you should be using*Time.delatTime

somewhere if you want it to move rather than “teleport” i think try this

var waiting : boolean = false;

function Update () 
{
	if (waiting == false)
	{
		LerpCube();
	}
		else
		{
			return;
		}
}


function LerpCube()
{
	var randomPosition = Vector3 (Random.Range( 6,-6 ), Random.Range( 4, -4 ));
	transform.position = Vector3.Lerp (transform.position*Time.deltaTime, randomPosition, 1);
	waiting = true;
	yield WaitForSeconds (1);
	waiting = false;

maybe that will work I havent realy done position transfoms much in a while but this i recon will do the job

That doesn’t really seem to change much. What value should I multiply with Time.deltaTime?

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 :smile:

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 :smile:

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");
}

That worked! Thanks!!

Happy to help I learned a few things myself heheh

oh wait that public var waiting should probably be set to private, I only had it that way for testing in the inspector.

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?