Help with movement code bugs

Godot Version

4.2.2

Question

currently my dashing is supposed to change the horizontal velocity
(hor_movement in code) it does do it but its really buggy and and stutters it might only be the camera bcs its a 3rd person one but i fixed it by making it not hit the player collision

i also want the controlled velocity to affect the uncontrolled one

thanks in advance :3

extends CharacterBody3D

#velocity upadted
var hor_movement : Vector3
var ver_movement : Vector3
var dash_movement : Vector3

#horizontal updated
var controlled_hmovement : Vector3
var uncontrolled_hmovement : Vector3

#player stats variables unused for now
var health : int = 10
var fuel : float = 1.00

#jump variables
var time_to_peak : float = 0.35
var j_height : float = 2.75
var descent : float = 0.3

var jumpvel : float = (2.0 * j_height) / time_to_peak
var grav : float = (-2.0 * j_height) / (descent * descent)
var jumpgrav : float = (-2.0 * j_height) / (time_to_peak * time_to_peak)

#move variables
var maxwalk : float = 8
var accel_walk : float = 1
var decel_walk : float = 0.8

#aerial variabels
var aerial_decel : float = 0.3

#dash variables
var dash_vel : float = 25
var is_dash : bool = false 
@onready var dash_time : Timer = $dash_time

#cam variables
var sens : float = 0.003
@onready var pivot : Node3D = $neck
@onready var giraffe_pivot : SpringArm3D = $neck/giraffe_pivot

func get_decel():
	return decel_walk if is_on_floor() else aerial_decel

func getgrav():
	if ver_movement.y > 0:
		return jumpgrav
	else:
		return grav

# Called when the node enters the scene tree for the first time.
func _ready():
	Input.mouse_mode = Input.MOUSE_MODE_CAPTURED

func _unhandled_input(event):
	if event is InputEventMouseMotion:
		pivot.rotate_y(-event.relative.x * sens)
		$neck/giraffe_pivot.rotate_x(-event.relative.y * sens)

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):

#walk movement
	var input_dir = Input.get_vector("a","d","w","s")
	var move_dir : Vector3 = (pivot.transform.basis * Vector3(input_dir.x,0,input_dir.y)).normalized()

	if move_dir:
		controlled_hmovement = controlled_hmovement.move_toward(maxwalk * move_dir,accel_walk)
	else:
		controlled_hmovement = controlled_hmovement.move_toward(Vector3.ZERO,get_decel())


#gravity
	if !is_on_floor():
		ver_movement.y += getgrav() * delta
	else:
		ver_movement.y = 0

#jump
	if Input.is_action_just_pressed("space") and is_on_floor():
		ver_movement.y += jumpvel

#dash stuff
	if Input.is_action_just_pressed("shift"):
		dash_time.start()

	if dash_time.is_stopped() == false:
		dash_movement = dash_vel * -pivot.global_basis.z
		is_dash = true
	else:
		dash_movement = Vector3.ZERO
		is_dash = false

	if is_dash == true and Input.is_action_just_pressed("space") and is_on_floor():
		dash_time.stop()
		uncontrolled_hmovement += dash_movement
		dash_movement = Vector3.ZERO
	uncontrolled_hmovement = uncontrolled_hmovement.move_toward(Vector3.ZERO,get_decel())

	hor_movement = controlled_hmovement + uncontrolled_hmovement
	velocity = hor_movement + ver_movement + dash_movement
	move_and_slide()

Without testing it, what could possible be happening that in the code above:

  • while dashing, every frame this code is called. It pretty hard sets the dash_movement depending on the mouse? You did this so you can “steer” the dash I would assume, which is cool but 60 calls per Frame could potentially cause the “stutter”. Maybe you can add a “smoothing” like you would normally do for movement, so it feels smoother. For example, adding a “steer” dash speed, and per each frame its applied so it feels smoother and not so stuttery

if is_dash == true and Input.is_action_just_pressed(“space”) and is_on_floor():
dash_time.stop()
uncontrolled_hmovement += dash_movement
dash_movement = Vector3.ZERO

This is the only condition where uncontrolled_hmovement += dash_movement happens is applied (on jump cancel I assume?).
You Probably want to call this always if the dash ends (if its like in this condition canceled) or if it runs out (currently not implemented). Maybe this will also fix the behaviour a bit.

Hope this helps!

Thanks for telling me the steering that is not intended but the stuttering doesn’t start while dashing it starts while im changing my uncontrolled movement direction to the dash direction it stutters and slows down (sorry for not including that I will very shortly)

I also need a way to affect uncontrolled movement from controlled movement (i don’t make them one big horizontal movement bcs of other bugs)

Thanks

Hey,
I think I am not able to help here, I dont fully understand what the problem is and what the goal is.

What would help me, to give you a better answer is maybe make a small video, of what is happening right now (the buggy behaviour), and what your goal would be.

Or create a minimal example which I can open in my Godot to check what is happening there.

Otherwise small tip what I would do, you have currently a lot of factors affecting the velocity and it seems in general buggy for you. Try to reduce some factors and variables and dont try to do everything at once. Try to remove the dash factor, the uncontrolled movement etc. for now and reduce your code (comment it out) until you have a working scene. Then you can start working with the working scene and adding back code to see what happens (where does the code start to fail).