Work in progress

The content of this page was not yet updated for Godot `4.2` and may be outdated. If you know how to improve this page or you can confirm that it's up to date, feel free to open a pull request.

# 使用代码移动玩家¶

```extends CharacterBody3D

# How fast the player moves in meters per second.
@export var speed = 14
# The downward acceleration when in the air, in meters per second squared.
@export var fall_acceleration = 75

var target_velocity = Vector3.ZERO
```

```func _physics_process(delta):
# We create a local variable to store the input direction.
var direction = Vector3.ZERO

# We check for each move input and update the direction accordingly.
if Input.is_action_pressed("move_right"):
direction.x += 1
if Input.is_action_pressed("move_left"):
direction.x -= 1
if Input.is_action_pressed("move_back"):
# Notice how we are working with the vector's x and z axes.
# In 3D, the XZ plane is the ground plane.
direction.z += 1
if Input.is_action_pressed("move_forward"):
direction.z -= 1
```

```#func _physics_process(delta):
#...

if direction != Vector3.ZERO:
direction = direction.normalized()
# Setting the basis property will affect the rotation of the node.
\$Pivot.basis = Basis.looking_at(direction)
```

We compute the direction the `\$Pivot` is looking by creating a Basis that looks in the `direction` direction.

```func _physics_process(delta):
#...
if direction != Vector3.ZERO:
#...

# Ground Velocity
target_velocity.x = direction.x * speed
target_velocity.z = direction.z * speed

# Vertical Velocity
if not is_on_floor(): # If in the air, fall towards the floor. Literally gravity
target_velocity.y = target_velocity.y - (fall_acceleration * delta)

# Moving the Character
velocity =```