i just changed the move and slide's stop on slope at third argument to true
this was before:
move_and_slide(velocity * move_speed * calculated_delta, Vector3.UP)
after:
move_and_slide(velocity * move_speed * calculated_delta, Vector3.UP, true)
it stop after half a second not inputing any key, but still works partially
this is the full code:
extends KinematicBody
#move
var velocity = Vector3.ZERO
var move_speed = 20
var move_strength_left
var move_strength_forward
#gravity
var fall_velocity = Vector3.ZERO
var fall_speed = 0.1
var jump_height = 2
#body and camera
onready var horizontal_head = $CamH
onready var vertical_head = $CamH/CamV
onready var body = $Body
var body_target_rotation = 0
var camera_rotation = 0
#inputs
var mouse_sensitivity = 0.05
var calculated_delta
var forward_key_input = 'w'
var backward_key_input = 's'
var left_key_input = 'a'
var right_key_input = 'd'
var aim_input = 'Rclick'
var jump_key_input = ' '
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _input(event):
if event is InputEventMouseMotion:
#horizontal
horizontal_head.rotate_y(deg2rad(-event.relative.x * mouse_sensitivity))
#vertical
vertical_head.rotate_x(deg2rad(-event.relative.y * mouse_sensitivity))
vertical_head.rotation.x=clamp(vertical_head.rotation.x, deg2rad(-89), deg2rad(89))
func _physics_process(delta):
#simplify
#forward
var forward = Input.is_action_pressed(forward_key_input)
var forward_strength = Input.get_action_strength(forward_key_input)
#backward
var backward = Input.is_action_pressed(backward_key_input)
var backward_strength = Input.get_action_strength(backward_key_input)
#left
var left = Input.is_action_pressed(left_key_input)
var left_strength = Input.get_action_strength(left_key_input)
#right
var right = Input.is_action_pressed(right_key_input)
var right_strength = Input.get_action_strength(right_key_input)
#right click
var right_click = Input.is_action_pressed(aim_input)
#jump
var jump = Input.is_action_pressed(jump_key_input)
#speed of 60 frames
calculated_delta = delta * 60
if forward || backward || left || right:
#calculate move
move_strength_left = left_strength - right_strength
move_strength_forward = forward_strength - backward_strength
#apply move
velocity -= horizontal_head.transform.basis.x * calculated_delta * move_strength_left
velocity -= horizontal_head.transform.basis.z * calculated_delta * move_strength_forward
#calculate rotation
if !right_click:
camera_rotation = horizontal_head.global_transform.basis.get_euler().y
#one key
if forward && !backward: body_target_rotation = 180
if backward && !forward: body_target_rotation = 0
if left && !right: body_target_rotation = -90
if right && !left: body_target_rotation = 90
#two keys
if forward && left: body_target_rotation = -135
if forward && right: body_target_rotation = 135
if backward && left: body_target_rotation = -45
if backward && right: body_target_rotation = 45
#mouse
if right_click:
camera_rotation = horizontal_head.global_transform.basis.get_euler().y
body_target_rotation = 180
#apply rotation
body.rotation.y = lerp_angle(body.rotation.y,
camera_rotation + deg2rad(body_target_rotation), 0.1 * calculated_delta)
#fall
if is_on_floor():
fall_velocity.y = 0
if jump:
fall_velocity.y = jump_height
if fall_velocity.y > -10:
fall_velocity.y -= fall_speed * calculated_delta
#normalize
velocity = velocity.normalized()
velocity += fall_velocity #merge
move_and_slide(velocity * move_speed * calculated_delta, Vector3.UP, true)