# How can I make an object change direction?

I have a moving cube that, when it moves over an arrow, should change its direction to the direction of that arrow. What would I need to do in order to accomplish this?

• Is this question about detecting intersection with the arrows or about changing the cube's movement angle? How is each arrow storing its angle? – Anko Sep 13 '14 at 19:05
• I want to create a simple code motion I just want my cube to detect the arrow and start moving in the direction it indicates. – Marcio Vinicius Sep 13 '14 at 22:31

Depending on how much arrows you have, you could simply check the distance from the arrows center to the cubes center and do something if it is below a threshhold.

//I assume ARROWS to hold the Transform of all arrows
foreach ( Transform arrow in ARROWS)
{
if ( (arrow.position - cube.position).magnitude  <= (Time.fixedDeltaTime * cube.speed) / 2)
cube.changeDirection (arrow.direction);
break;
}


This will ensure, that the cube moves as close as possible to an arrow before changing direction.

ATTENTION:
I totally made up the fields speed, changeDirection () and direction. You have to define them yourself or derive them from the existing data.

The best place for this would most likely be the fixedUpdate() of your cube.