- Home /
object pauses at the each way points for a vary short time
I write this code.
var waypoint:Transform [];
internal var currentWay : int=0;
var TransformSpeed:float;
var duration:float;
var RotationSpeed:float;
function Start () {
}
function Update () {
var target:Transform =waypoint[currentWay];
transform.rotation=Quaternion.Slerp(
this.transform.rotation,target.rotation,Time.smoothDeltaTime*RotationSpeed);
transform.position=Vector3.Slerp(
transform.position,target.position,
Time.smoothDeltaTime*TransformSpeed);
}
function OnTriggerEnter (other : Collider) {
currentWay++;
}
I have an array of way points: "waypoint[]". so my car when trigger each waypoint go the next way point. but something odd will happen . the speed of my car isn't fixed. and each time reach to the waypoint . it is vary slow or even I think stop, and then go vary fast. exactly like our heart Pulse. " fast->normal->vary slow->fast->...." in the other word it is not Continuously , like a real car.
I don't know but I think it is related to the lerp function. can you help me?
Answer by Seregon · Jul 05, 2013 at 12:28 PM
A lerp/slerp function will move your car a fraction of the distance between two points, in this case its current position and its next waypoint. As your car gets closer to each waypoint, that distance gets smaller, so the fraction of that distance that it moves each update is also smaller, and it moves slower. To get a fixed speed, you can can specify a fixed speed, and divide that by the distance left to the next waypoint to get the fraction (t) you need to move this update, something like the example below. Also, you should be using Vector3.Lerp, not .Slerp, as slerp is intended for use with cartesion rotations, not positions.
function Update () {
var target:Transform =waypoint[currentWay];
transform.rotation=Quaternion.Slerp(this.transform.rotation,
target.rotation,Time.smoothDeltaTime*RotationSpeed);
var distanceToWayPoint = (target.position - transform.position).magnitude;
transform.position=Vector3.Slerp(
transform.position,target.position,
Time.smoothDeltaTime*(TransformSpeed / distanceToWayPoint));
}
Thank you. That is exactly what I want. in The overall , is it a good way to implement the simple 2d top down Ai car? is there any better way?
Your answer
Follow this Question
Related Questions
Rotate a set amount over time WITHOUT lerping 0 Answers
How do you smoothly transition/Lerp into a new rotation? 3 Answers
transform.localRotation lerp precision 1 Answer
Unit rotation fails consistently on all slerp rotations after the first? 0 Answers
How to rotate 90 degrees from 270 degrees to 0 with lerp? c# 1 Answer