Godot Version
Godot 4
Question
I’m currently making a 3D fps game similar to Pizza Tower in movement. I have the acceleration mechanic for my game, but I add something else to it as well. I want to make sure whenever you’re holding down the shift key and you turn around, that you skid on the ground. The player will still keep their speed they had but will skid on the ground just like Pizza Tower. However, I’m unsure how to do this exactly with my CharacterBody3D code.
Here is my current code:
var speed
const WALK_SPEED = 10.0
const SPRINT_SPEED = 20.0
const JUMP_VELOCITY = 7.8
var accelerationx = 5.0
var dead = false
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = 18.8
@onready var camera = $Camera3D
@onready var animated_sprite_2d = $"CanvasLayer/Bare Hands/AnimatedSprite2D"
func _ready():
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
camera.current = true
func _unhandled_input(event: InputEvent) -> void:
if dead:
return
if event is InputEventMouseButton:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
elif event.is_action_pressed("ui_cancel"):
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
if event is InputEventMouseMotion:
rotate_y(-event.relative.x * 0.005)
camera.rotate_x(-event.relative.y * 0.005)
camera.rotation.x = clamp(camera.rotation.x, -PI/4, PI/3)
func _physics_process(delta):
var input_dir = Input.get_vector("left", "right", "forward", "backward")
var direction = (camera.transform.basis * transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if not is_on_floor():
velocity.y -= gravity * delta
# Handle Jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Handle Sprint.
if Input.is_action_pressed("sprint") and Input.get_vector("left", "right", "forward", "backward"):
speed = move_toward(speed, SPRINT_SPEED, accelerationx * delta)
else:
speed = WALK_SPEED
if speed == 20:
animated_sprite_2d.play("Running")
elif speed == 0 or speed == WALK_SPEED:
animated_sprite_2d.play("Idle")
if is_on_floor():
if direction:
velocity.x = direction.x * speed
velocity.z = direction.z * speed
else:
velocity.x = lerp(velocity.x, direction.x * speed, delta * 7.0)
velocity.z = lerp(velocity.z, direction.z * speed, delta * 7.0)
else:
velocity.x = lerp(velocity.x, direction.x * speed, delta * 3.0)
velocity.z = lerp(velocity.z, direction.z * speed, delta * 3.0)
move_and_slide()
And here is a video demonstrating the acceleration mechanic: