I'm just building my first ever FPS in godot and it's early days. I have hacked together my player controller from a variety of websites and youtube videos but all the information ranges across various years and Godot versions, so wanted to ask is all this adhering to currect best practices? Is there anything here I could make more efficient?
Thanks to all who take the time to look at this 🙂
extends KinematicBody
export var speed = 10
export var accel = 10
export var gravity = 50 # gravity strength when off ground
export var jump = 22
export var sensitivity = 0.2 #mouse sensitivity looking around
export var max_angle = 90 # used for clamp
export var min_angle = -80 # used for clamp
onready var head = $Head
onready var camera = $Head/Camera
var look_rot = Vector3.ZERO
var move_dir = Vector3.ZERO
var velocity = Vector3.ZERO
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _physics_process(delta):
head.rotation_degrees.x = look_rot.x
rotation_degrees.y = look_rot.y
if Input.is_action_pressed("jump") and is_on_floor():
velocity.y = jump
if not is_on_floor():
velocity.y -= gravity * delta
move_dir = Vector3(
Input.get_action_strength("right") - Input.get_action_strength("left"),
0,
Input.get_action_strength("backward") - Input.get_action_strength("forward")
).normalized().rotated(Vector3.UP, rotation.y)
velocity.x = lerp(velocity.x, move_dir.x * speed, accel * delta)
velocity.z = lerp(velocity.z, move_dir.z * speed, accel * delta)
velocity = move_and_slide(velocity, Vector3.UP)
func _input(event):
if event is InputEventMouseMotion:
look_rot.y -= (event.relative.x * sensitivity)
look_rot.x -= (event.relative.y * sensitivity)
look_rot.x = clamp(look_rot.x, min_angle, max_angle)
if Input.is_action_just_pressed("run"):
speed *= 3
print("running")
if Input.is_action_just_released("run"):
speed = 10
print("walking")