- Home /
Facing moving direction using Waypoints
I'm using the following waypoint system but I am unable to get the characters to face the direction the are moving in. Can anyone be of assistance in telling me how to do so?
var Waypoint : Transform[]; var speed : int = 20; private var currentWP : int; var loop : boolean = true;
function Update() { if(currentWP < Waypoint.length){ var target = Waypoint[currentWP].position; var moveDirection : Vector3 = target - transform.position;
var velocity = rigidbody.velocity;
if (moveDirection.magnitude < 1){ currentWP++; } else { velocity = moveDirection.normalized * speed; } } else{ if(loop){ currentWP=0; } else{ velocity = Vector3.zero; } } rigidbody.velocity = velocity;
}
@script RequireComponent(Rigidbody)
Answer by Jessy · Feb 04, 2011 at 09:43 PM
It looks like scripting a rigidbody is worthless to your code; if you actually need a rigidbody, then use forces. Use Transform.LookAt(waypoints[currentWaypointIndex]), and apply relative force in the Z direction.
(waypoints should be lowercase and currentWaypointIndex is more descriptive of the variable.)
There's one problem, it's looking at the point it just passed and not the point that's co$$anonymous$$g up.
If I try to set it to negative then it completely screws up the facing direction.
Your answer
Follow this Question
Related Questions
Can the animation editor create local rotational data? 3 Answers
Different animation on each wayPoint 1 Answer
Adding animation clips via script 2 Answers