How to make a simple first person controller? 3D

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By VinnieH01
:warning: Old Version Published before Godot 3 was released.

Im having problem making a first person camera (you can look around, move etc) and learning the godot languge in general because there’s very few tutorials out there. I thought that maybe someone here could help me out with making it, or linking me to a tutorial.

:bust_in_silhouette: Reply From: Calinou

You can find examples here and also here, the latter is a bit more robust (it handles jumping better). Both are based on a RigidBody character.

:bust_in_silhouette: Reply From: Gokudomatic2

Last year I wrote a tutorial about FPS, that is I guess what you’re looking for:

Project:

It is already an old tutorial (sorry about that but godot is always evolving). If you want, you can check the current project I’m working on, which uses my controller on a recent godot version:

Note: my controller uses some concepts of already existing fps controllers, but I rewrite it from scratch with a kinematic body instead of a rigid body. My version doesn’t have the problem of bouncing when going down a steep slope. And it manages moving platforms (staying on the floor when the lift goes down).

If you need help about it, don’t hesitate to ask me.

I didn’t know this tutorial, it’s so useful thanks!

sergicollado | 2016-04-18 15:35

:bust_in_silhouette: Reply From: Dimredcm

Use this code and make sure you have a node called “Head” that’s either a camera or a parent of a camera.

extends KinematicBody

var speed = 8
var ground_acceleration = 8
var air_acceleration = 2
var acceleration = ground_acceleration
var jump = 4.5
var gravity = 9.8
var stick_amount = 10
var mouse_sensitivity = 1

var direction = Vector3()
var velocity = Vector3()
var movement = Vector3()
var gravity_vec = Vector3()
var grounded = true

func _input(event):
	if event is InputEventMouseMotion:
		rotation_degrees.y -= event.relative.x * mouse_sensitivity / 10
		$Head.rotation_degrees.x = clamp($Head.rotation_degrees.x - event.relative.y * mouse_sensitivity / 10, -90, 90)

	direction = Vector3()
	direction.z = -Input.get_action_strength("move_forward") + Input.get_action_strength("move_backward")
	direction.x = -Input.get_action_strength("move_left") + Input.get_action_strength("move_right")
	direction = direction.normalized().rotated(Vector3.UP, rotation.y)

func _physics_process(delta):
	if is_on_floor():
		gravity_vec = -get_floor_normal() * stick_amount
		acceleration = ground_acceleration
		grounded = true
	else:
		if grounded:
			gravity_vec = Vector3.ZERO
			grounded = false
		else:
			gravity_vec += Vector3.DOWN * gravity * delta
			acceleration = air_acceleration
	
	if Input.is_action_just_pressed("jump") and is_on_floor():
		grounded = false
		gravity_vec = Vector3.UP * jump
	
	velocity = velocity.linear_interpolate(direction * speed, acceleration * delta)
	movement.z = velocity.z + gravity_vec.z
	movement.x = velocity.x + gravity_vec.x
	movement.y = gravity_vec.y
	
	move_and_slide(movement, Vector3.UP)