Why am i accelerating faster in the negative axis then the positive axis

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:

image

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()

I think this is where the problem is.

if velocity.length() > SPEED:
  velocity = velocity.normalized() * SPEED

Although it could be okay, i don’t see a glaring issue your video isn’t clear. I would add a text label showing the velocity length, aka speed. I wonder if the input is somehow registering left and back longer then right and forward.

updated the video! and yes the clamp is the problem

this is the movement without the clamp

shamefully
if velocity.length() > SPEED:
velocity = velocity.normalized() * SPEED
dont fix it

i fixed it by doing this!

velocity.x = clamp(velocity.x, -abs(speed * direction.x), abs(speed * direction.x))
velocity.z = clamp(velocity.z, -abs(speed * direction.z), abs(speed * direction.z))

apparently sometimes the clamp returns (velocity.x, 25, 25) and vice versa making the speed suddenly changing in the negative axis

2 Likes

Okay, i did think of that, because the either axis could be negative. Which would make it positive on the min parameter and negative in the max… Ha i guess i didn’t really think about it to hard, thinking clamp would still work.

And ah, my code, this is 3d of course it would mess up the y axis unless it was taken care of.

1 Like

Thank you very much!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.