- Home /
waypoint system help
Hi, i've been having trouble figuring out how to add a way for the rigid-body to stop at a specific waypoint then continue on to the next waypoint.
here is the code so far.
var WalkSpeed = 1.0;
var speed : float = 5.0;
private var currentWaypoint : int;
var loop = true;
var waypoint : Transform[];
function Start(){
animation["walk"].wrapMode = WrapMode.Loop;
}
function Update(){
animation["walk"].speed = WalkSpeed;
if(currentWaypoint < waypoint.length){
var target : Vector3 = waypoint[currentWaypoint].position;
var moveDirection : Vector3 = target - transform.position;
var velocity = rigidbody.velocity;
if(moveDirection.magnitude < 1){
currentWaypoint++;
}
else{
velocity = moveDirection.normalized*speed;
animation.Play("walk");
}
}
else{
if(loop){
currentWaypoint = 0;
}
else{
velocity = Vector3.zero;
}
}
rigidbody.velocity = velocity;
transform.LookAt(target);
}
Answer by sestren · May 09, 2012 at 05:22 PM
What exactly is the problem? At a quick glance this code should work for moving between waypoints. Do you simply need the object to stop as it approaches a waypoint and then continue on after a set period of time?
If that's the case then you can just add a check in this section:
if(moveDirection.magnitude < 1){
currentWaypoint++;
}
Before you increment the currentWaypoint value simply set a value to the current Time.time. Afterwords check to see if the difference between the current Time.time and your base start value exceeds your pause duration. If it does, continue on to the next waypoint.
if(moveDirection.magnitude < 1){
if (curTime == 0)
curTime = Time.time;
if ((Time.time - curTime) >= pauseDuration) {
currentWaypoint++;
curTime = 0;
}
else {
velocity = Vector3.zero;
}
}
Setting the velocity straight to zero is obviously not the best way to handle a "pause", but it works for the sake of this example. You would probably want to smoothly move the value down toward zero depending on the situation (and consequently, push up the waypoint detection range from 1 to something higher).
Ah, that almost what was ai$$anonymous$$g for but not exactly.(sorry for not being specific enough)
let me try to convey better what i had in $$anonymous$$d: i was wondering about storing specific waypoints so that the object would stop at those specific ones, otherwise it would just continue moving along the given path.
thank you for your help.
Same idea. You would check in that same IF block if the 'currentWaypoint' matches any value in a list of pause waypoints. How you do that depends on how you want the pauses to work... If you want it to stop at EVERY saved location you would iterate over the list and compare each value to the current waypoint to see if it matches. If you wanted to compare it only to it's exact counterpart in the corresponding list then you could do it without looping and just using the index. If the waypoint values match just set the velocity to zero (or gradually decline) as I did in the example above.
the first if checks the distance, the second checks if curTime is 0, if not it sets it to Time.time. Then it checks if Time.time-curTime (rightnow this is 0)>pauseDuration so passed the if and do the else. velocity is assigned Vector3.zero. ok, but next round does exactly the same since I do not see where curTime is changed.
var curTime:float;
if(moveDirection.magnitude < 1){
curTime= Time.deltaTime;
if (curTime >= pauseDuration) {
currentWaypoint++;
assignementOfNewTarget();
curTime = 0;
}
// This part not so useful as this section does not have any moving code.
else {
velocity = Vector3.zero;
}
}