Why is my lerp going faster on my npc the closer he gets to the player

so ive been using lerp for my npc movement and his movement speed is set to a number and shouldn’t change I even made it a constant and it still changes speed based on player distance here is the npc and the code:
extends AnimatableBody2D
@onready var raycast = $RayCast2D
const MOVE_SPEED = 10
var inray = false
@onready var sprite = $AnimatedSprite2D

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _process(delta: float) → void:
var location: Vector2 = raycast.get_collision_point()
if raycast.is_colliding():
sprite.play(“run”)
position.x = lerp(position.x, location.x, delta * MOVE_SPEED)

if raycast.is_colliding() == false:
	$AnimatedSprite2D.play("idle") 

if location.x - position.x > 0:
	sprite.flip_h = false
elif location.x - position.x < 0:
	sprite.flip_h = true

lerp’s third parameter is a percentage of how much a or b to return, if 0.0 it will return a, if 1.0 it will return b, if 0.5 it will return a value between a and b. In your code it does not move by pixels as you want, it moves by a percentage of the distance between the two points.

Written out lerp is as follows

func lerp(a, b, t: float) -> float:
    return a + (b - a) * t

You want to use move_toward, and you will have to increase your MOVE_SPEED to be a good pixels per second value, like 200.

position.x = move_toward(position.x, location.x, delta * MOVE_SPEED)
1 Like

thanks

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.