This is my script, it only goes left. How can I make it go left, then right?
asked Jan 07 '11 at 08:36 AM
Your object is moving left because you have a vector (-1,0,0) in your script. To make the object move right you'd use a vector (1.0f, 0.0f, 0.0f).
To make the object move left and then right, you need to decide when to make the switch. That could either be when the x value of the position is less than a certain value:
or based on time, in which case you'd use Time.time.
However, typically in a game you want your objects to be moving and changing direction all the time. With this more complex requirement you are really better off using a simple state machine to control your object. The state could be a simple variable that takes on one of 3 values (0, 1 or 2) which mean the object is not moving, moving left, or moving right:
This code just controls how the object moves. You can now have other code that sets the value of state, to control how the object moves. You could for example have an OnGUI function that has three buttons you can press to control which direction your object moves in. In your real game, however, you'd compute the state based on lots of things including time, where the object is, where an AI system wants the object to go to, or what keys, mouse or touch the player makes.
answered Jan 07 '11 at 12:02 PM
Graham Dunnett ♦♦