feat: character actor velocity is now applied only when on the floor

This commit is contained in:
Sara 2024-03-31 18:15:47 +02:00
parent d3b9e54b0b
commit 56f0e3bc87

View file

@ -34,9 +34,9 @@ void CharacterActor::_process(double delta_time) { GDGAMEONLY();
void CharacterActor::_physics_process(double delta_time) { GDGAMEONLY();
// accelerate towards velocity target
Vector3 const new_velocity = this->get_velocity().move_toward(this->velocity_target, delta_time * CharacterActor::ACCELERATION);
// only apply velocity if not grounded
Vector3 const gravity{this->is_on_floor() ? Vector3() : Vector3{0.f, this->get_velocity().y - 9.8f, 0.f}};
this->set_velocity(new_velocity + gravity);
Vector3 const gravity{Vector3{0.f, this->get_velocity().y - 9.8f, 0.f}};
// apply either gravity or walking velocity depending on results
this->set_velocity(this->is_on_floor() ? new_velocity : this->get_velocity() + gravity);
// update position
this->move_and_slide();
}