Relative character movement

This commit is contained in:
Rob Kelly 2024-07-20 15:27:33 -06:00
parent aff0c7c791
commit ae3ea2ac1c
3 changed files with 36 additions and 22 deletions

View File

@ -1,31 +1,48 @@
extends CharacterBody3D extends CharacterBody3D
const TARGET_FPS: float = 60.0
const SPEED = 5.0 const BASE_SPEED: float = 5.0
const JUMP_VELOCITY = 4.5 const JUMP_FORCE: float = 4.5
const FRICTION: float = 0.3
const AIR_DRAG: float = 0.03
const INPUT_SENSITIVITY: float = 0.7
const TURN_SENSITIVITY: float = 0.08
# Get the gravity from the project settings to be synced with RigidBody nodes. # Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity") var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity")
@onready var camera_root: Node3D = $CameraRoot
@onready var mesh: Node3D = $Mesh
func _physics_process(delta: float) -> void: func _physics_process(delta: float) -> void:
var delta_factor: float = delta * TARGET_FPS
# Add the gravity. # Add the gravity.
if not is_on_floor(): if not is_on_floor():
velocity.y -= gravity * delta velocity.y -= gravity * delta
# Handle jump. # Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor(): if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY velocity.y = JUMP_FORCE
# Get the input direction and handle the movement/deceleration. # Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions. var input_dir: Vector2 = Input.get_vector("left", "right", "forward", "backward")
var input_dir := Input.get_vector("left", "right", "forward", "backward") if input_dir:
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() var movement: Vector3 = (
if direction: camera_root.global_transform.basis * Vector3(input_dir.x, 0, input_dir.y) * -1
velocity.x = direction.x * SPEED ).normalized()
velocity.z = direction.z * SPEED velocity.x = lerpf(velocity.x, movement.x * BASE_SPEED, delta_factor * INPUT_SENSITIVITY)
else: velocity.z = lerpf(velocity.z, movement.z * BASE_SPEED, delta_factor * INPUT_SENSITIVITY)
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED) mesh.rotation.y = lerp_angle(
mesh.rotation.y,
atan2(movement.x, movement.z),
delta_factor * TURN_SENSITIVITY
)
var drag: float = FRICTION if is_on_floor() else AIR_DRAG
velocity.x = lerpf(velocity.x, 0.0, delta_factor * drag)
velocity.z = lerpf(velocity.z, 0.0, delta_factor * drag)
move_and_slide() move_and_slide()

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=8 format=3 uid="uid://bqp5mwyat3asv"] [gd_scene load_steps=7 format=3 uid="uid://bqp5mwyat3asv"]
[ext_resource type="Script" path="res://src/player/player.gd" id="1_h16ke"] [ext_resource type="Script" path="res://src/player/player.gd" id="1_h16ke"]
[ext_resource type="Texture2D" uid="uid://b0yefajw1cqo" path="res://assets/textures/character_placeholder.png" id="1_iys0p"] [ext_resource type="Texture2D" uid="uid://b0yefajw1cqo" path="res://assets/textures/character_placeholder.png" id="1_iys0p"]
@ -11,8 +11,6 @@ albedo_texture = ExtResource("1_iys0p")
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_otw5j"] [sub_resource type="CapsuleShape3D" id="CapsuleShape3D_otw5j"]
[sub_resource type="SphereShape3D" id="SphereShape3D_jv2hv"]
[node name="Player" type="CharacterBody3D"] [node name="Player" type="CharacterBody3D"]
script = ExtResource("1_h16ke") script = ExtResource("1_h16ke")
@ -27,13 +25,12 @@ surface_material_override/0 = SubResource("StandardMaterial3D_dd4i1")
shape = SubResource("CapsuleShape3D_otw5j") shape = SubResource("CapsuleShape3D_otw5j")
[node name="CameraRoot" type="Node3D" parent="."] [node name="CameraRoot" type="Node3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 0.866025, -0.5, 0, 0.5, 0.866025, 0, 0.5, 0) transform = Transform3D(1, 0, 0, 0, 0.965926, -0.258819, 0, 0.258819, 0.965926, 0, 1, 0)
script = ExtResource("3_gichr") script = ExtResource("3_gichr")
[node name="SpringArm3D" type="SpringArm3D" parent="CameraRoot"] [node name="SpringArm3D" type="SpringArm3D" parent="CameraRoot"]
transform = Transform3D(-1, 0, 8.74228e-08, 0, 1, 0, -8.74228e-08, 0, -1, 0, 0, 0) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -1, 0, 0)
shape = SubResource("SphereShape3D_jv2hv") spring_length = -1.5
spring_length = 3.0
[node name="Camera3D" type="Camera3D" parent="CameraRoot/SpringArm3D"] [node name="Camera3D" type="Camera3D" parent="CameraRoot/SpringArm3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 3) transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 0, -1.5)

View File

@ -10,7 +10,7 @@ extends Node3D
@export_range(-90.0, 90.0) var pitch_max: float = 75.0 @export_range(-90.0, 90.0) var pitch_max: float = 75.0
@export var invert_pitch: bool = false @export var invert_pitch: bool = false
@onready var _target: Vector2 = Vector2(rotation_degrees.x, rotation_degrees.y) @onready var _target: Vector2 = Vector2(global_rotation_degrees.x, rotation_degrees.y)
func _ready() -> void: func _ready() -> void:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED Input.mouse_mode = Input.MOUSE_MODE_CAPTURED