Godot Version
4.3
Question
my velocity somehow always accelerate faster in the negative axis, be it X or Z.
heres the video:
heres my node tree:
heres my code:
extends CharacterBody3D
enum States {IDLE, MOVING, JUMPING, FALLING}
const SENS = deg_to_rad(3)
const SPEED = 25
const GROUND_FRICTION = 5
const ACCELERATION = 80
var state = States.IDLE
@onready var pivot = $Pivot
func _ready():
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
func _input(event):
# Mouse camera movement
if event is InputEventMouseMotion:
rotate_y(deg_to_rad(-event.relative.x * SENS))
pivot.rotate_x(deg_to_rad(-event.relative.y * SENS))
pivot.rotation.x = clamp(pivot.rotation.x, deg_to_rad(-70), deg_to_rad(90))
# Quit with escape key
if Input.is_action_just_pressed(“quit”):
get_tree().quit()
func _physics_process(delta):
# Get movement input
var input_dir = Input.get_vector(“left”, “right”, “up”, “down”)
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
# If input movement not equal zero, state = moving
if direction != Vector3.ZERO:
state = States.MOVING
else:
state = States.IDLE
# If jump is pressed change state to jump!
if Input.is_action_just_pressed("jump"):
state = States.JUMPING
# If is not on floor, falls
if not is_on_floor():
state = States.FALLING
# Moves the character based on direction
if state == States.MOVING:
velocity.x += direction.x * ACCELERATION * delta
velocity.z += direction.z * ACCELERATION * delta
# Clamps the max speed
velocity.x = clamp(velocity.x, -SPEED * direction.x, SPEED * direction.x)
velocity.z = clamp(velocity.z, -SPEED * direction.z, SPEED * direction.z)
# Stops the character when idle
if state == States.IDLE:
velocity = lerp(velocity, Vector3(0, velocity.y, 0), GROUND_FRICTION * delta)
# Handle jump input
if state == States.JUMPING:
velocity.y = 25
if state == States.FALLING:
velocity.y -= 50 * delta
print(velocity)
move_and_slide()