0 votes

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.

in Engine by (20 points)

3 Answers

+3 votes
Best answer

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


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.

by (697 points)
selected by

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

0 votes

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.

by (11,255 points)
0 votes

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
        if grounded:
            gravity_vec = Vector3.ZERO
            grounded = false
            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)
by (14 points)
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.