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?

enter image description here

  • 1
    \$\begingroup\$ Is this question about detecting intersection with the arrows or about changing the cube's movement angle? How is each arrow storing its angle? \$\endgroup\$ – Anko Sep 13 '14 at 19:05
  • \$\begingroup\$ 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. \$\endgroup\$ – 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);

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

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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.