- Home /
transform.position vector3.lerp appears to stay still for a while before the 5DP matches the new location
My goal is to make an enemy who moves to a location, shoots at the player, waits a few seconds and then repeats the process for 3-5 times before it becomes vulnerable. i just finished scripting the movement, however, when the enemy moves to the new location, it isnt actually at that location until the 5 or so decimal points of each x,y,z position of the current location is in line with the new location. this makes it appear to be at the location for about 5 or so seconds, sometimes 10+ seconds, before moving. Below is the script im using.
var newLoc : Vector3;
var curLoc : Vector3;
var speed : float = 100;
function Start()
{
newLoc = Vector3(Random.Range(1.75,8.25),Random.Range(13.5,15.5),0);
}
function Update()
{
curLoc = transform.position;
if(curLoc == newLoc)
{
newLoc = Vector3(Random.Range(1.75,8.25),Random.Range(14.5,15.5),0);
}
else if(curLoc != newLoc)
{
transform.position = Vector3.Lerp(curLoc, newLoc, Time.deltaTime * speed);
}
}
i tried to round the locations to 2decimal points to help speed up the lerp process, however i couldnt figure out how to do that to the current position (transform.position) of the object. heres the code i used to set newLoc to 2dps
newLoc = Vector3(Mathf.Round(Random.Range(1.75,8.25)*100)/100,Mathf.Round(Random.Range(14.5,15.5)*100)/100,0);
What can i do to fix the problem of waiting in the same spot for it to realize im already there before it moves again?
Answer by robertbu · Feb 23, 2014 at 09:56 AM
The problem is an issue when Lerp() is used this way. This form of Lerp() moves towards the goal some small fraction of the distance each frame. The fraction varies a bit because Time.deltaTime varies, but essentially you can think of it as the same fraction each frame. As the distance drops, the same fraction of the remaining distance represents ever decreasing movement each frame.
It's like the old question: if I walk half the distance to my goal, when will I reach my goal? The answer: never.
There are two easier solutions to your problem, and many more complex ones. The easiest solution is to add a distance check and move the object to the goal if the distance falls below some threshold. The object reaches close to the goal relative quickly:
if (Vector3.Distance(curLoc, newLoc) < 0.02) {
transform.position = newLoc;
}
Adjust the '0.02' as appropriate to your situation.
The second easy solution is to change from using Vector3.Lerp() to using Vector3.MoveTowards(). MoveTowards() does not ease the movement, but since it moves at a fixed speed, it arrives promptly (with respect to the whole movement) at the destination. All you have to do is change Lerp() to MoveTowards() and adjust the 'speed' parameter as appropriate.
If neither of these two solutions work for you, I'll present a more complex solution.
Thankyou very much for clarifying, the first solution is was exactly what i was looking for, works great.