- Home /
How will i make my objects/model(say a car) face the way points and travel towards it,instead of blindly following it?
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.
Answer by save · Aug 01, 2013 at 12:26 PM
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.
thanks for ur help
using UnityEngine;
using System.Collections;
public class waypoint : $$anonymous$$onoBehaviour {
public Transform[] waaypoint;
public int speed=50;
private int currentwaypoint;
public Vector3 velocity;
// Use this for initialization
void Start () {
//Debug.Log(currentwaypoint);
}
// Update is called once per frame
void Update () {
//Debug.Log(currentwaypoint);
if(currentwaypoint< waaypoint.Length)
{
Vector3 target= waaypoint[currentwaypoint].position;
Vector3 movedirection=target-transform.position;
velocity=rigidbody.velocity;
if(movedirection.magnitude<1)
{
currentwaypoint++;
}
else
{
velocity=movedirection.normalized*speed;
}
}
rigidbody.velocity= velocity;
transform.LookAt(waaypoint[currentwaypoint].position);
if(currentwaypoint == waaypoint.Length)
{
// ??
}
}
}
this is the code that i used ....it works fine but my car still continues to move even after reaching the last waypoint.
moreover, i hav a simple terrain with small mountains , the car just after a few elevated movement moves through the mountain to the last waypoint.
how do i solve this ?
Answer by Hamilcar-games · Sep 12, 2017 at 10:21 AM
who likes the c# from the answer code this is the code :
Vector3 direction = targetPosition - transform.position;
Quaternion newlook_rotation = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Slerp(transform.rotation, newlook_rotation, rotation_speed * Time.deltaTime);
Your answer
![](https://koobas.hobune.stream/wayback/20220613111335im_/https://answers.unity.com/themes/thub/images/avi.jpg)