I'm trying to create a 3D prototype where players can build up momentum by performing certain moves to increase their velocity. I've got a basic structure down, but I think my logic is wrong somewhere because it isn't working quite right, as seen in the video.
I don't know the best practices for handling velocity in this kind of scenario and I was hoping someone could show me how it's typically done.
# Handles acceleration.
func accelerate(target_velocity: float, delta: float) -> void:
acceleration_velocity = velocity.move_toward((movement_direction * target_velocity), (acceleration * delta))
velocity.x = acceleration_velocity.x
velocity.z = acceleration_velocity.z
# Handles deceleration.
func decelerate(delta: float) -> void:
acceleration_velocity = velocity.move_toward(Vector3.ZERO, (friction * delta))
velocity.x = acceleration_velocity.x
velocity.z = acceleration_velocity.z
# Handles horizontal movement when applicable.
func _allow_movement(delta: float) -> void:
var current_speed: float = velocity.length()
movement_direction = (transform.basis * Vector3(input_direction.x, 0.0, input_direction.y).normalized())
movement_direction = movement_direction.rotated(Vector3.UP, camera.rotation.y)
if movement_direction:
if current_speed < input_velocity:
accelerate(input_velocity, delta)
else:
velocity.x = (movement_direction.x * current_speed)
velocity.z = (movement_direction.z * current_speed)
model.rotation.y = lerp_angle(model.rotation.y, atan2(movement_direction.x, movement_direction.z), (rotation_speed * delta))
else:
decelerate(delta)