# how to make the player's speed dependent on health?

Attention Topic was automatically imported from the old Question2Answer platform.

in short: “the less health, the slower you move”
the player’s code is written like in a typical top-down shooter
I just want to make a horror game where the player’s speed will play an important role.

here’s some code:

``````const ACCELERATION = 700
const FRICTION = 1000
const MAX_SPEED = 95

var velocity = Vector2.ZERO
-----------------------------
export (int) var start_hp : int = 3;
var can_take_damage = true;

func take_damage():
if (can_take_damage):
can_take_damage = false;
hp -= 1;
animationFrisk.play("Hit");
else:
return;
``````

Hi,
you can write a simple function to calculate this:

``````func linear_blend( state_in:float, from_in:float, to_in:float, from_out:float, to_out:float ):
return from_out + (to_out - from_out) * ((state_in - from_in) / (to_in - from_in))
``````

where:
state_in = the current hitpoints
from_in = the lowest hitpoints (ths hp where the character is the slowest)
to_in = the highest hitpoints (the hp where the player is at full speed)
from_out = the lowest acceleration
to_out = the highest acceleration
returns the current acceleration

this will blend/return the acceleration in relation to the hitpoints

GDScript already has a `range_lerp` function that does just this

Lola | 2021-07-20 11:43