i’m a newbie and i’m trying out some stuff with a few models.
I have a car model and when i was trying to use waypoints to make it follow throught those points ,my car isn’t facing towards it .
Instead it is trying to just move towards the waypoint no matter what side it is turned .
I want the car’s front side to be facing towards the waypoint it is trying to reach.
To get a direction from two positions the basic calculation is like this:
target.position - transform.position
To set a new look rotation from a direction over time (with linear interpolation):
var rotationSpeed : float = 10.0;
var direction : Vector3 = Vector3(target.position - transform.position);
var newLookRotation : Quaternion = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Slerp(transform.rotation, newLookRotation, rotationSpeed*Time.deltaTime);
There’s also a function called RotateTowards which can be useful. If you don’t want to rotate over time and just set the rotation immediately, you can use LookAt.
transform.LookAt(target);
So what you basically have to do is to assign each next waypoint as your target when you’ve finished the current. As you haven’t posted any script, an example:
var allWayPoints = Transform[];
var currentWayPoint : int;
var target : Transform;
// Called when reaching current waypoint
function FinishedWaypoint () {
// Increase currentWayPoint by one and make it loop if exceeds length of all waypoints
currentWayPoint++; currentWayPoint = currentWayPoint%allWayPoints.Length;
// Set next waypoint as new target (to later rotate towards it)
target = allWayPoints[currentWayPoint];
}
On another note, I saw you had duplicate posts in the moderation queue. There might be a little bit of delay of the publication of your questions as you have to wait for approval from a moderator - just so you don’t think that your question got lost.