Godot Version
Using Godot Version 4.2.
Experience
Beginner, I barely have a clue as to what I’m doing
Question
Hello! I am trying to work on a 3D platformer, taking cues specifically from Sonic Adventure in particular, and I have been using a CharacterBody3D. I’ve been using a modified version of the default sample code, and I’m not sure how to get character acceleration and deceleration working smoothly.
Here’s a snippet of the character movement code, if needed I can provide the full code:
var MAXSPEED = 30.0
var SPEED = 0.0
var JUMP_VELOCITY = 16.0
var DECEL = 0.40
var ACCEL = 0.20
func _physics_process(delta):
# Add the gravity
if not is_on_floor():
velocity.y -= gravity * delta
var input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
var direction = (twist_pivot.basis * transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
# Speed up acceleration
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
if SPEED < MAXSPEED:
SPEED = SPEED + ACCEL
print("SPEED:",SPEED)
elif SPEED > 0:
# Slow down
SPEED = SPEED - DECEL
velocity.x = move_toward(velocity.x, SPEED, 0)
velocity.z = move_toward(velocity.z, SPEED, 0)
print("SPEED:",SPEED)
else:
# Stop
SPEED = 0
velocity.x = move_toward(velocity.x, 0, MAXSPEED)
velocity.z = move_toward(velocity.z, 0, MAXSPEED)
move_and_slide()
If anybody could provide a explanation as to how I could polish up the acceleration/deceleration, please do tell me. As well, if the full code is needed: I can provide that as well.