81 lines
2.3 KiB
GDScript
81 lines
2.3 KiB
GDScript
extends CharacterBody3D
|
|
class_name Player
|
|
|
|
@export var speed = 5.0
|
|
@export var jump_velocity = 4.5
|
|
@export var mouse_sensitivity = 0.002
|
|
|
|
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
|
|
var camera_node: Camera3D
|
|
|
|
|
|
|
|
|
|
|
|
func _ready():
|
|
if is_multiplayer_authority():
|
|
print("-> [%s] Authority granted. Setting up camera and input." % name)
|
|
camera_node = $Camera3D
|
|
camera_node.make_current()
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
|
$Mesh.hide()
|
|
$Label3D.hide()
|
|
|
|
|
|
func set_player_name(peer_id: String, player_name: String):
|
|
name = peer_id
|
|
$Label3D.text = player_name
|
|
|
|
|
|
#@rpc("any_peer", "call_local", "unreliable")
|
|
#func update_remote_transform(new_transform: Transform3D):
|
|
#global_transform = new_transform
|
|
|
|
|
|
func _physics_process(delta):
|
|
if is_multiplayer_authority():
|
|
# Apply gravity
|
|
if not is_on_floor():
|
|
velocity.y -= gravity * delta
|
|
|
|
# Handle Jump
|
|
if Input.is_action_just_pressed("jump") and is_on_floor():
|
|
velocity.y = jump_velocity
|
|
|
|
# Get the input direction and apply movement
|
|
var input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
|
|
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
|
|
|
|
if is_on_floor():
|
|
if direction:
|
|
velocity.x = direction.x * speed
|
|
velocity.z = direction.z * speed
|
|
else:
|
|
velocity.x = move_toward(velocity.x, 0, speed)
|
|
velocity.z = move_toward(velocity.z, 0, speed)
|
|
else:
|
|
# Air control
|
|
velocity.x = lerp(velocity.x, direction.x * speed, delta * 5.0)
|
|
velocity.z = lerp(velocity.z, direction.z * speed, delta * 5.0)
|
|
move_and_slide()
|
|
|
|
#update_remote_transform.rpc(global_transform)
|
|
|
|
|
|
func _input(event):
|
|
if is_multiplayer_authority():
|
|
if event is InputEventMouseMotion:
|
|
# Rotate the CharacterBody3D around the Y-axis for horizontal look
|
|
rotate_y(-event.relative.x * mouse_sensitivity)
|
|
|
|
# Rotate the Camera3D around its local X-axis for vertical look
|
|
var change = -event.relative.y * mouse_sensitivity
|
|
var new_x_rotation = camera_node.rotation.x + change
|
|
camera_node.rotation.x = clamp(new_x_rotation, deg_to_rad(-90), deg_to_rad(90))
|
|
|
|
if event.is_action_pressed("ui_cancel"): # Typically Escape key
|
|
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
|
|
else:
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|