I’ve been trying for a while to write the code from this example in gdscript but I cant wrap my head around how I would do it. I know you can use C# in godot but I want to be able to understand the code in my game and I think this would help me. If I figure out how to do this I’ll post it here, but for right now I have no clue.
I’d like to also say I don’t know if this is the right place to put this question, so if this is the wrong place correct me.
# accelDir: normalized direction that the player has requested to move (taking into account the movement keys and look direction)
# prevVelocity: The current velocity of the player, before any additional calculations
# accelerate: The server-defined player acceleration value
# max_velocity: The server-defined maximum player velocity (this is not strictly adhered to due to strafejumping)
const friction: float = 0.0
const ground_accelerate: float = 0.0
const max_velocity_ground: float = 0.0
const air_accelerate: float = 0.0
const max_velocity_air: float = 0.0
func Accelerate(accelDir: Vector3, prevVelocity: Vector3, accelerate: float, max_velocity: float, delta_from_physics_process: float) -> Vector3:
var projVel: float = prevVelocity.dot(accelDir); # Vector projection of Current velocity onto accelDir.
var accelVel: float = accelerate * delta_from_physics_process; # Accelerated velocity in direction of movment
# If necessary, truncate the accelerated velocity so the vector projection does not exceed max_velocity
if projVel + accelVel > max_velocity:
accelVel = max_velocity - projVel
return prevVelocity + accelDir * accelVel
func MoveGround(accelDir: Vector3, prevVelocity: Vector3, delta_from_physics_process: float) -> Vector3:
# Apply Friction
var speed: float = prevVelocity.length()
if not is_zero_approx(speed): # To avoid divide by zero errors
var drop: float = speed * friction * delta_from_physics_process
prevVelocity *= maxf(speed - drop, 0) / speed # Scale the velocity based on friction.
# ground_accelerate and max_velocity_ground are server-defined movement variables
return Accelerate(accelDir, prevVelocity, ground_accelerate, max_velocity_ground, delta_from_physics_process)
func MoveAir(accelDir: Vector3, prevVelocity: Vector3, delta_from_physics_process: float) -> Vector3:
# air_accelerate and max_velocity_air are server-defined movement variables
return Accelerate(accelDir, prevVelocity, air_accelerate, max_velocity_air, delta_from_physics_process);