Patrol unit lose speed when get close to target
Hello everyone, thank you for the support.
I was learning on youtube how to make an 2d RPG, and everything was fine since i tried to make some changes in the code.
I had to implement a NPC that move from A to B, and vice versa. The mechanic of doing this is fine, it goes and back normally. The problem is the speed, i noticed that the longer the npc is from target, the faster it is, and when it gets closer, it gets slow as a snail lol.
I searched on internet and discovered that it is a comum problem, but nothing solved for me.
The code below is inside of FixedUpdate(). I don´t know so much, i'm kind new on unity, so tried everything. Changed to update(), tried moveTowards instead lerp, but i got the same result.
Can someone help me please?! Thank you!!
Obs.: The change goal is a fucntion that changes the target game object only.
var heading_path = path[current_point].position - this.transform.position;
heading_path.z = 0;
var point_distance = heading_path.magnitude;
var direction_path = heading_path / point_distance; // This is now the normalized direction.
Vector3 temp2 = Vector3.Lerp(this.transform.position, path[current_point].position, 0.005f);
Vector3 temp3 = new Vector3(temp2.x, temp2.y, 0);
//anda
anim.SetBool("wake", false);
anim.SetBool("walk", true);
anim.SetBool("attack", false);
anim.SetBool("sleep", false);
//temp.z = 16;
transform.position = temp3;
Change_anim(direction_path);
if (point_distance <= 2)
{
Change_goal();
}
Answer by KoenigX3 · Jun 03, 2020 at 08:56 AM
Lerp functions take 3 parameters: a start point, an end point, and an interpolant. If the interpolant is 0, the function returns the start point. If the interpolant is 1, the function returns the end point. If the interpolant is between 0 and 1, the function returns a point between the start and end point.
Normally, to make the interpolation linear, you would need a static starting point and a dynamic interpolant. In your case, you are using the Lerp function in a different way.
Your starting point is not static, which means that it changes every frame.
Your interpolant is not dynamic, which means that it is constant.
By doing so, you are achieving a decreasing interpolation. The function returns a point between the start and end point about 0.5% from the start. Since you are using the current position (this.transform.position) as the starting point, the distance between the start and end point decreases, and since you are always returning 0.5% of the distance, it decelerates.
The solution is to use a fixed starting point and an increasing interpolant.
Vector3 startingPoint;
float interpolant = 0;
void Update()
{
Vector3 temp2 = Vector3.Lerp(startingPoint, path[current_point].position, interpolant);
interpolant += Time.deltaTime;
}
Whenever you change the end point, you also have to change the start point to the current position (but only once, of course), and the interpolant to 0.
Your answer
Follow this Question
Related Questions
How to revert speed back to normal after changing with Key. 1 Answer
Something is wrong with my max. speed code 0 Answers
Increase variable? 0 Answers
[SOLVED] When touch and drag objects fast (in android), leave dragging. 1 Answer
Need help, the speed stacks if I press left/up, right/up, left/down or right/down. 1 Answer