|
@@ -23,7 +23,7 @@ character.
|
|
|
|
|
|
# How fast the player moves in meters per second.
|
|
|
@export var speed = 14
|
|
|
- # The downward acceleration while in the air, in meters per second squared.
|
|
|
+ # The downward acceleration when in the air, in meters per second squared.
|
|
|
@export var fall_acceleration = 75
|
|
|
|
|
|
var target_velocity = Vector3.ZERO
|
|
@@ -39,7 +39,7 @@ character.
|
|
|
// How fast the player moves in meters per second.
|
|
|
[Export]
|
|
|
public int Speed = 14;
|
|
|
- // The downward acceleration while in the air, in meters per second squared.
|
|
|
+ // The downward acceleration when in the air, in meters per second squared.
|
|
|
[Export]
|
|
|
public int FallAcceleration = 75;
|
|
|
|
|
@@ -47,7 +47,7 @@ character.
|
|
|
}
|
|
|
|
|
|
|
|
|
-These are common properties for a moving body. The ``velocity`` is a :ref:`3D vector <class_Vector3>`
|
|
|
+These are common properties for a moving body. The ``target_velocity`` is a :ref:`3D vector <class_Vector3>`
|
|
|
combining a speed with a direction. Here, we define it as a property because
|
|
|
we want to update and reuse its value across frames.
|
|
|
|
|
@@ -186,8 +186,8 @@ fall speed separately. Be sure to go back one tab so the lines are inside the
|
|
|
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)
|
|
|
+ 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 = target_velocity
|
|
@@ -237,27 +237,27 @@ Here is the complete ``Player.gd`` code for reference.
|
|
|
.. tabs::
|
|
|
.. code-tab:: gdscript GDScript
|
|
|
|
|
|
- extends CharacterBody3D
|
|
|
+ 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
|
|
|
+ # 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
|
|
|
+ var target_velocity = Vector3.ZERO
|
|
|
|
|
|
|
|
|
- func _physics_process(delta):
|
|
|
- var direction = Vector3.ZERO
|
|
|
+ func _physics_process(delta):
|
|
|
+ var direction = Vector3.ZERO
|
|
|
|
|
|
- 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"):
|
|
|
- direction.z += 1
|
|
|
- if Input.is_action_pressed("move_forward"):
|
|
|
- direction.z -= 1
|
|
|
+ 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"):
|
|
|
+ direction.z += 1
|
|
|
+ if Input.is_action_pressed("move_forward"):
|
|
|
+ direction.z -= 1
|
|
|
|
|
|
if direction != Vector3.ZERO:
|
|
|
direction = direction.normalized()
|
|
@@ -268,8 +268,8 @@ Here is the complete ``Player.gd`` code for reference.
|
|
|
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)
|
|
|
+ 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 = target_velocity
|