Here is an example of a first person controller script in GDScript for Godot Engine 3.x and how it can be applied to a KinematicBody node:


extends KinematicBody

var velocity = Vector3()
var speed = 10

func _physics_process(delta):
    velocity = Vector3()

    if Input.is_action_pressed("move_forward"):
        velocity.z -= 1
    if Input.is_action_pressed("move_backward"):
        velocity.z += 1
    if Input.is_action_pressed("move_left"):
        velocity.x -= 1
    if Input.is_action_pressed("move_right"):
        velocity.x += 1

    velocity = velocity.normalized() * speed
    move_and_slide(velocity)

To apply this script to a KinematicBody node in your scene, create a new KinematicBody node, right-click it, and select "Attach Script." Then paste the script into the new script file and save it.

To make this script work in Godot Engine 4.x, the script needs to be updated to use the new API, which includes some syntax changes and changes to how physics are processed. Here is an updated version of the script that should work in Godot Engine 4.x:

extends KinematicBody

var velocity = Vector3.ZERO
var speed = 10

func _physics_process(delta):
    velocity = Vector3.ZERO

    if Input.is_action_pressed("move_forward"):
        velocity.z -= 1
    if Input.is_action_pressed("move_backward"):
        velocity.z += 1
    if Input.is_action_pressed("move_left"):
        velocity.x -= 1
    if Input.is_action_pressed("move_right"):
        velocity.x += 1

    velocity = velocity.normalized() * speed
    move_and_slide(velocity)

Note that in Godot Engine 4.x, the KinematicBody node has been renamed to RigidBody, and the move_and_slide method has been replaced with move.
spam fails_task lang_mismatch pii not_appropriate hate_speech sexual_content quality toxicity humor helpfulness creativity violence