Set velocity based on distance.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By 9BitStrider

I’m programming a boss that I want to be able to jump on the player. I want to set his X velocity based on how far away he is from the player. The farther away, the faster he travels horizontally. What’s the best way to calculate this?

:bust_in_silhouette: Reply From: Magso

Use distance to multiplied by speed.

overall_speed = boss.distance_to(player.position)*speed

and use overall_speed variable. Don’t put this in _process because distance_to will cause it to slow down .

You could also use lerp.

boss_pos = boss.position

while time < 1:
    boss.position = lerp(boss_pos, player.position, time)
    time += speed
    yield(get_tree(), "idle_frame")