Simple Waypoints

Hi,

I am trying to figure out the way to do very simple way points. Move to the waypoint, when there, move to next one. Look at image below.

I am sorry for bad english.

How would I go about doing this?

Thanks

Hi,
you can do this using a scheduler. This scheduler will take in a avatar that you are going to move. In addition, you will need to have waypoints to form the path as you have indicated in your picture. So you will need to declare a array of waypoints to form the path for the avatar to follow. You will need to declare a parameter: time taken for the avatar to complete the trip. Then you need to determine the trigger to make the avatar follow the path. The avatar will follow the waypoints in a sequence that is specified in the array of waypoints of the scheduler. Therefore, you need to check if the avatar is moving, that it has reach one of the waypoints so that it will move onto the next one. And upon reaching the last one, you will stop the movement. This also means you probably need a flag to indicate if the avatar is moving or not.

I placed some code below to help. It will not compile but the idea is in there.

class PathFollower
{
//the time taken for the avatar to move
public float timeTaken;
// a bunch of waypoints. basically game objects
public GameObject [ ] waypoints;
//the target game object that you want to move
public GameObject avatar;
//a flag to indicate if the avatar is still moving
public bool isMoving;
//the current waypoint index. This allows you to keep track of the distance between the last waypoint you visited and the next one
public int currentWaypointIndex;
//the previous waypoint index
public int previousWaypointIndex;
//the distance threshold before you consider that the avatar has reach the next point
public float distanceThreshold;
//the time so far for moving the avatar
public float timeSoFar;

public void Start ()
{…some init code…}

public void Update ()
{

//check out if the waypoint has been reached
if (isMoving){

//update the position of the avatar using deltaTime
Vector3 direction = waypoints [currentWaypoint].transform.position - waypoints[previousWaypoint].transform.position;
timeSoFar += Time.deltaTime;
avatar.transform.position.x = Mathf.Lerp (avatar.transform.position.x,waypoints [currentWaypoint].transform.position.x,timeSoFar/timeTaken);
… do the same for y and z…

if (Vector3.Distance (avatar.transform.position, waypoints [currentWaypoint].transform.position) < distanceThreshold)
{
//check if the next waypoint exists or not
if ( (currentWaypoint + 1) >= waypoints.Length){
//do not add one and set the moving to false
isMoving = false;
//that is all
return;
}

//increment both waypoint indexes by 1 if the waypoint to go beyond the list of waypoints
currentWaypoint ++;
previousWaypoint ++;
}
}
}
}

There are still plenty of maintenance stuff I missed out here like how can we trigger the movement. Again, a navigation system is pretty much easy to find on Unify website if you are looking for some ready made modules to play with. Hope this helps.

Dacosi’s post covers it. The navigator I wrote is very similar except is use a vector3.Lerp instead of Mathf.Lerp. The difference being vector3.lerp is a single call for moving from one vector to the other rather than a single value to another.

An alternative to the movement code is to use an animation framework such as iTween. The iTween website has plenty of examples.

If you need to have your gameobject navigate the scene from one waypoint to the next then you will need to have some sort of pathfinding. A couple of links that maybe of interest:
AngryAnt’s Path system
Aron Granberg’s A* Pathfinding