Help With 3D Movement Acceleration

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By manameter

Basically, I can’t seem to get acceleration and deceleration to work on my kinematic body at all. I’m really new to this, so maybe it’s obvious, but in theory what I’ve written should work? don’t know why it isn’t.

code:

extends KinematicBody

export var speed := 30.0
export var jump_strength := 25.0
export var gravity := 70.0
export var rotation_speed := 20.0
export var acceleration := 45.0
export var friction := 35.0



var _velocity := Vector3.ZERO
var _snap_vector := Vector3.DOWN
var _linear_velocity = Vector3()

onready var _spring_arm: SpringArm = $SpringArm
onready var _model: Spatial = $COOLGUYbeta


func _physics_process(delta: float) -> void:
	var move_direction := Vector3.ZERO
	
	var _vv = _linear_velocity.y # Vertical velocity.
	var _hv = Vector3(_linear_velocity.x, 0, _linear_velocity.z) # Horizontal velocity.

	var _hdir = _hv.normalized() # Horizontal direction.
	var _hspeed = _hv.length() # Horizontal speed.
	
	
	move_direction.x = Input.get_action_strength("right") - Input.get_action_strength("left")
	
	move_direction.z = Input.get_action_strength("back") - Input.get_action_strength("forward")
	
	move_direction = move_direction.rotated(Vector3.UP, _spring_arm.rotation.y).normalized()
	
	if is_on_floor():
		if _hspeed < speed:
			_hspeed += acceleration * delta
	
		else:
			_hspeed -= friction * delta
			if _hspeed < 0:
				_hspeed = 0
	
	
	
	print(_hspeed)
	
	
	_velocity.x = move_direction.x * speed
	_velocity.z = move_direction.z * speed
	_velocity.y -= gravity * delta
	
	


	
	var just_landed := is_on_floor() and  _snap_vector == Vector3.ZERO
	var is_jumping := is_on_floor() and Input.is_action_just_pressed("jump")
	if is_jumping:
		_velocity.y = jump_strength
		_snap_vector = Vector3.ZERO
	elif just_landed:
		_snap_vector = Vector3.DOWN
	_velocity = move_and_slide_with_snap(_velocity, _snap_vector, Vector3.UP, true)
	if Vector2(_velocity.z, _velocity.x).length() > 0.2:
		var look_direction = Vector2(_velocity.z, _velocity.x)
		_model.rotation.y = lerp_angle(_model.rotation.y, look_direction.angle(), delta * rotation_speed)


func _process(_delta: float) -> void:
	_spring_arm.translation = translation
:bust_in_silhouette: Reply From: Bernard Cloutier

You’re not even using _hspeed, you’re just setting the velocity to speed (which should be named max_speed, maybe that would have helped you not confuse them).

so i changed this:

_velocity.x = move_direction.x * speed
    _velocity.z = move_direction.z * speed
    _velocity.y -= gravity * delta

to this:

_velocity.x = move_direction.x * _hspeed
    _velocity.z = move_direction.z * _hspeed
    _velocity.y -= gravity * delta

thinking that that was the issue but now it just moves at a fixed rate that is determined by my acceleration variable. dunno exactly what i should do instead. but i do appreciate the help regardless, pointing me in the right direction

manameter | 2022-08-08 17:13

What do you mean “fixed rate”? Is it not increasing ip to max speed?

Edit: nevermind, found your issue. You have two different velocity variable. You are getting the speed from one (which is always at zero) and then setting the other. Just use velocity and remove linear velocity.

Bernard Cloutier | 2022-08-08 18:10

nope. it just starts and stays at the value determined by my acceleration variable.

manameter | 2022-08-08 18:11

I’ve edited my comment, I think I’ve found the issue.

Bernard Cloutier | 2022-08-08 18:13

hahahaha yep that was it! thanks. the dangers of referencing two separate examples of movement code. I really appreciate the help!

manameter | 2022-08-08 18:17