first person character controller godot code example
Example: godot first person controller script
extends KinematicBody
onready var camera = $Pivot/Camera
var gravity = -30
var max_speed = 8
var mouse_sensitivity = 0.002 # radians/pixel
var velocity = Vector3()
func get_input():
var input_dir = Vector3()
# desired move in camera direction
if Input.is_action_pressed("move_forward"):
input_dir += -camera.global_transform.basis.z
if Input.is_action_pressed("move_back"):
input_dir += camera.global_transform.basis.z
if Input.is_action_pressed("strafe_left"):
input_dir += -camera.global_transform.basis.x
if Input.is_action_pressed("strafe_right"):
input_dir += camera.global_transform.basis.x
input_dir = input_dir.normalized()
return input_dir
# Set "move_forward" to w, or up arrow and etc
# Check out https://kidscancode.org/godot_recipes/g101/3d/101_3d_07/ for more info