How to make a gameObject move from it's current position in a different direction after a set amount of time

EDITED:

Hello everyone.

I have a sphere that I am moving using rigidbody.velocity. I would like to after a set time in this case 5 seconds tell the sphere to move in a new direction. For example the sphere starts moving to the left,after 5 seconds are up the sphere starts moving upwards. So after every 5 seconds that pass the sphere keeps changing it’s movement direction.

In order to achieve this I was thinking that I could keep track of the sphere’s position throughout its travel time.Then when the seconds are up I assign the position it is at as the new position.Another words I want to have the sphere take it’s current position as the new starting point from which it moves off.

I tried to do this by tracking the position of the sphere at run time and the after 5 seconds are up assigning it to a new variable.But that has gotten me nowhere.

This is my timer code:

function LateUpdate () 
{
	print("late update");
	
	ParticleMovement();///Function that controls sphere movement	
	
	if(movementTimer > 0)
	{
		movementTimer -= Time.deltaTime;
		print(movementTimer);
	}
	
	if(movementTimer <= 0)
	{
		ParticleMovement();//I'm calling the movement function here when timer
                               //reaches zero so that the particles direction
                               //is chosen again.
		movementTimer = newTimer;//Reset the timer back to 5 seconds every time
	}
}

All hints are welcome :smiley:

Thank you.

Your newPosition and currentPosition values probably will never be exactly the same value, which is what is being tested in your example. Especially, if the movement is performed overtime. In stead of checking if newPosition is currentPosition, I’d suggest calculating the distance between the two points, and if the returned distance is less than an acceptable margin the currentPosition is ‘close enough’ to the new position. From there if you really want you could snap the currentPosition to your newPosition if needed.