Attention | Topic was automatically imported from the old Question2Answer platform. | |
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