Hopefully I haven’t missed something too obvious here. I have a route contained in a List. I would like an object to move from point A to point B and then from point B to point C and so on at a constant rate. Lerp works but it slows down as it gets closer to it’s destination. I tried rounding down the position floats so that things don’t slow down as much but then the motion is somewhat jumpy. Is there a better way?
Here’s what I have so far.
var SetBikeRoute : List.<GameObject> = new List.<GameObject>();
var BikeStart = false;
var OnStartRoute = true;
var WhichLegOfRoute = 0;
var CurrentDestination : GameObject;
var smooth = 1.0;
var CurX = 0.0;
var TargX = 0.0;
var CurY = 0.0;
var TargY = 0.0;
var NumRound = 10;
function Update () {
if (BikeStart){
//send Bike to start
if (OnStartRoute){
transform.position = SetBikeRoute[0].transform.position;
OnStartRoute = false;
}
//Limit floats to speed up transitions
CurX = Mathf.Round(transform.position.x*NumRound)/NumRound;
CurY = Mathf.Round(transform.position.y*NumRound)/NumRound;
TargX = Mathf.Round(SetBikeRoute[WhichLegOfRoute].transform.position.x*NumRound)/NumRound;
TargY = Mathf.Round(SetBikeRoute[WhichLegOfRoute].transform.position.y*NumRound)/NumRound;
//Set Which Leg Of Route
if (CurX == TargX CurY == TargY){
WhichLegOfRoute++;
//Check if bike at destination
if (WhichLegOfRoute == SetBikeRoute.Count){
BikeStart = false;
OnStartRoute = true;
//indicate bike arrived
}
else {
//set next leg
CurrentDestination = SetBikeRoute[WhichLegOfRoute];
}
}
//move to position
transform.position = Vector3.Lerp (transform.position, CurrentDestination.transform.position, Time.deltaTime * smooth);
}
}
OK, I discovered 1 error. I should be using my start and end points for the Lerp and not my current position. Unfortunately now I have a new problem. The object seems to stay at the starting point in a vibrating state.
var SetBikeRoute : List.<GameObject> = new List.<GameObject>();
var BikeStart = false;
var OnStartRoute = true;
var WhichLegOfRoute = 0;
var PreviousPosition : GameObject;
var NextPosition : GameObject;
var smooth = 2.0;
function Update () {
if (BikeStart){
//send Bike to start
if (OnStartRoute){
transform.position = SetBikeRoute[0].transform.position;
OnStartRoute = false;
}
//Set Which Leg Of Route
if (transform.position == SetBikeRoute[WhichLegOfRoute].transform.position){
//set next leg
PreviousPosition = SetBikeRoute[WhichLegOfRoute];
WhichLegOfRoute++;
//Check if bike at destination
if (WhichLegOfRoute == SetBikeRoute.Count){
BikeStart = false;
OnStartRoute = true;
//indicate bike arrived
}
else {
NextPosition = SetBikeRoute[WhichLegOfRoute];
}
}
//move to position
transform.position = Vector3.Lerp (PreviousPosition.transform.position, NextPosition.transform.position, Time.deltaTime * smooth);
}
}
Got it! I found a script Eric5h5 wrote in Answers and managed to make it work for my situation. Thanks again Eric.
#pragma strict
var SetBikeRoute : List.<GameObject> = new List.<GameObject>();
var BikeStart = false;
var OnStartRoute = true;
var WhichLegOfRoute = 0;
var PreviousPosition : GameObject;
var NextPosition : GameObject;
var i = 0.0;
var speedrate = 2.0;
function Update () {
if (BikeStart){
//send Bike to start
if (OnStartRoute){
transform.position = SetBikeRoute[0].transform.position;
OnStartRoute = false;
PreviousPosition = SetBikeRoute[WhichLegOfRoute];
WhichLegOfRoute++;
i = 0.0;
}
//Set Which Leg Of Route
if ( i > 1.0){
//set next leg
PreviousPosition = SetBikeRoute[WhichLegOfRoute];
WhichLegOfRoute++;
i = 0.0;
//Check if bike at destination
if (WhichLegOfRoute == SetBikeRoute.Count){
BikeStart = false;
OnStartRoute = true;
//indicate bike arrived
}
else {
NextPosition = SetBikeRoute[WhichLegOfRoute];
}
}
//move to position
i += Time.deltaTime * 1.0/speedrate;
transform.position = Vector3.Lerp (PreviousPosition.transform.position, NextPosition.transform.position, i);
}
}