Waypoint Follow

Hello its me again,

i have a character and it has 4 waypoint ( front, rear, left, right).
i want the AI to go to the rear waypoint if it faces the rear waypoint,
go to the front waypoint if the AI faces the front waypoint, and so on.
hope you understand me guys. thanks.

here is the current script i have. the script follow only one waypoint.
i have watched tutorials on waypoints but it only teaches going one waypoint to another.

var speed : float = 1; 
var rotateSpeed : float = 1;
var Rot : Quaternion; 
private var cwaypoint:int;
var waypoint:Transform[];

function Update(){ 

if(cwaypoint<waypoint.length){
	var target:Vector3=waypoint[cwaypoint].position;
	var moveDirection:Vector3=target-transform.position;	
//move towards waypoint
	if(moveDirection.magnitude<1){
	cwaypoint++;
}
	else {
	var delta = target - transform.position;
	delta.Normalize();
	var moveSpeed = speed * Time.deltaTime;
	transform.position = transform.position + (delta * moveSpeed);
//Rotate Towards
	Rot = Quaternion.LookRotation(target - transform.position,Vector3.up);
	transform.rotation = Quaternion.Slerp(transform.rotation, Rot,Time.deltaTime * rotateSpeed);	

		}
	}
}

I think you need the direction vector3 vPlayer of you’re player
then calculate the direction vectors off all youre waypoints from the player to the waypoint. (vector3 vWPx = waypoint.position - player.position)
Then compare each vWPx vector to the vPlayer vector.
in the case of the player being in the middle of the waypoints, the player vector would point to the opposite direction.

maybe you could give this a try…

umm can you give me a simpler explanation? i really dont get what you say and also the scripts that you’re using.

well maybe you can explain it to me a bit more. What i would do. Is this:

  • create a array off waypoints
  • pick one random waypoint out off the array and name this TargetWaypoint
  • rotate the character so its faces the TargetWayPoint
  • let character move to the TargetWaypoint
  • If the character arrives at the TargetWaypoint remove the waypoint you picked earlier from the array of waypoints (this is to avoid that you pick this waypoint again)
  • Name the current TargetWaypoint tempWaypoint
  • Then pick one random waypoint out off the array again and name this TargetWaypoint
  • add the tempWaypoint to the array

then go to point 3 in th above cycle…

ill explain it again. i have a character right. i will place 4 waypoints: front, rear, leftside, rightside.
my ai will follow the character. when the AI is near the front waypoint it will go directly to the waypoint and same goes for the other.
do you get me? thanks for the help btw.

I don’t fully understand what your after but Arges Unity Steer component might be worth investigating as a robust solution to your problem.

These are static waypoints? Not dynamic. I believe the instructions they are giving you were for dynamic ones. So the player would have 4 waypoints around him (and move with him) Then it would pick a point and move to it, kind of like a steering. (hence why they are pointing you towards the steering.

What I belive that you are really asking for is a way to check a facing towards a certain waypoint if so, turn and move towards that waypoint.

That being said, that is not all that you wish to do here. (according to your code), you also want it to jump to the next waypoint once it reaches that waypoint. It also may need a little tweaking as you go.

What I did was to modify your code a bit, first to get the “I am facing this waypoint, so lets go to it” part, then simplify the selection of the next waypoint.

I did not modify how your moving or rotating.

There are a few things I would change in it. Like going to a random waypoint, or going to a point within a sphere or circle around that waypoint, instead of directly to it. Also, you could write some code to include terrain in that. (raycast from the position that you are at using x and z) Arges Unity Steer has alot of great examples and a fully complete addon. GitHub - ricardojmendez/UnitySteer at v2.0.0

var speed : float = 1; 
var rotateSpeed : float = 1;
var rot : Quaternion; 
var waypoints:Transform[];

function Update(){
//find which waypoint we are looking most towards
//a modification of the Vector3.Dot example
	var forward = transform.TransformDirection(Vector3.forward);
	var facings=new Array();
	for(var i=0; i<waypoints.length;  i++){
		var toOther = waypoints[i].position - transform.position;
		facings.push(Vector3.Dot(forward,toOther));
	}
//find the highest Dot product (will be the one we are facing most)
	var waypoint=0;
	var max=-1.0;
	for(i=0; i<facings.length; i++){
		if(facings[i]>max){
			max=facings[i];
			waypoint=i;
		}
	}
	
//if we reach our way point, just select the next waypoint, and start towards it.
	var target:Vector3=waypoints[waypoint].position;
	var moveDirection:Vector3=target-transform.position;
	
//make sure we have the right waypoint data (if updated)
	if(moveDirection.magnitude<1){waypoint=(waypoint+1) % waypoints.length;
	target=waypoints[waypoint].position;
	moveDirection=target-transform.position;
	
//move towards waypoint
		moveDirection.Normalize();
		var moveSpeed = speed * Time.deltaTime;
		transform.position = transform.position + (moveDirection * moveSpeed);
		
//Rotate Towards
		rot = Quaternion.LookRotation(target - transform.position,Vector3.up);
		transform.rotation = Quaternion.Slerp(transform.rotation, rot,Time.deltaTime * rotateSpeed);	
	}
}

Thanks BigMisterB. what im looking for is the “i am looking at this waypoint. i must go there.”. no need for the go to other waypoint. thanks. thank you very much. also thank MMWizard and devans770 for the helpful site.

Just going to a single point is trivial. The calculation would be something like this:

Which you would then need to adjust by your vehicle’s maximum speed and the elapsed time on the current frame. If you actually want to follow a path of points, then this would be more relevant to your interests:

https://github.com/ricardojmendez/UnitySteer/blob/b2.1/Behaviors/SteerToFollowPath.cs
https://github.com/ricardojmendez/UnitySteer/blob/b2.1/Vector3Pathway.cs

Cheers,

Well no thanx… I was thinking more like a character moving to fixed waypoints… So i wasnt of any help…
I hope you’ve found the solution…
BTW Have fun tonight you all…