+1 vote
extends KinematicBody

const speed = 10
const acceleration = 5
const gravity = 9.8
const jump = 15
const mouse_sensitivity = 0.31

onready var head = $Head
onready var camera = $Head/Camera

 var velocity = Vector3()
 var camera_x_rotation = 0

 func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

 func _input(event):
if event is InputEventMouseMotion:
    head.rotate_y(deg2rad(-event.relative.x * mouse_sensitivity))

    var x_delta = event.relative.y * mouse_sensitivity
    if camera_x_rotation + x_delta > -90 and camera_x_rotation + x_delta < 90:
        camera.rotate_x(deg2rad(-x_delta))
        camera_x_rotation += x_delta

func _process(_delta):
if Input.is_action_just_pressed("ui_cancel"):
    Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)

func _physics_process(_delta):
var head_basis = head.get_global_transform().basis

var direction = Vector3()
if Input.is_key_pressed(KEY_W):
    direction -= head_basis.z
elif Input.is_key_pressed(KEY_S):
    direction += head_basis.z
elif Input.is_key_pressed(KEY_A):
    direction -= head_basis.x
elif Input.is_key_pressed(KEY_D):
    direction += head_basis.x

direction = direction.normalized()

velocity = velocity.linear_interpolate(direction * speed, acceleration * _delta)
velocity.y -= gravity

if Input.is_action_just_pressed("jump") and is_on_floor():
    velocity.y += jump

velocity = move_and_slide(velocity, Vector3.UP)
Godot version 3.2.4
in Projects by (178 points)

1 Answer

+1 vote
Best answer

i believe you want a slowdown instead of an abrupt stop when you stop moving.

For that, replace :
velocity = velocity.linear_interpolate(direction * speed, acceleration * _delta)

with :
if direction.length() == 0: //no input?
. velocity = velocity * friction
else:
. velocity = velocity.linear_interpolate(direction * speed, acceleration * _delta)

With const friction being around 0.95.

by (2,092 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories