fully sync player movements over network
This commit is contained in:
@@ -3,7 +3,7 @@ extends CharacterBody3D
|
||||
const DIST_FLOOR_SNAP := Vector3.DOWN * 0.2
|
||||
|
||||
@onready var label : Label = $CanvasLayer/Label
|
||||
@onready var synchroniser = $Networking/MultiplayerSynchronizer
|
||||
@onready var networking = $Networking
|
||||
|
||||
@onready var body : Node3D = $Body
|
||||
@onready var head : Node3D = $Body/Head
|
||||
@@ -29,12 +29,15 @@ const DIST_FLOOR_SNAP := Vector3.DOWN * 0.2
|
||||
|
||||
var floor_snap := Vector3.ZERO
|
||||
|
||||
func _ready():
|
||||
synchroniser.set_multiplayer_authority(str(name).to_int())
|
||||
cam.current = is_local_authority()
|
||||
func _enter_tree():
|
||||
$Networking/MultiplayerSynchronizer.set_multiplayer_authority(str(name).to_int())
|
||||
$Body/Head/Camera3D.current = is_local_authority()
|
||||
if is_local_authority():
|
||||
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
||||
|
||||
|
||||
func is_local_authority():
|
||||
return synchroniser.get_multiplayer_authority() == multiplayer.get_unique_id()
|
||||
return $Networking/MultiplayerSynchronizer.get_multiplayer_authority() == multiplayer.get_unique_id()
|
||||
|
||||
func _process(_delta):
|
||||
if not is_local_authority():
|
||||
@@ -42,6 +45,7 @@ func _process(_delta):
|
||||
label.text = "H Velocity: %3.2f" % [Vector2(velocity.x, velocity.z).length()]
|
||||
label.text += "\nV Velocity: %3.2f" % [velocity.y]
|
||||
label.text += "\nOn floor: %s" % is_on_floor()
|
||||
label.text += "\nNetwork ID: %d" % multiplayer.get_unique_id()
|
||||
|
||||
func get_move_direction() -> Vector3:
|
||||
var input_dir := Vector2(
|
||||
@@ -62,7 +66,10 @@ func rotate_look(amount : Vector2) -> void:
|
||||
|
||||
func _physics_process(delta):
|
||||
if !is_local_authority():
|
||||
|
||||
if not networking.processed_position:
|
||||
position = networking.sync_position
|
||||
networking.processed_position = true
|
||||
velocity = networking.sync_velocity
|
||||
move_and_slide()
|
||||
return
|
||||
|
||||
@@ -96,6 +103,9 @@ func _physics_process(delta):
|
||||
floor_snap = DIST_FLOOR_SNAP
|
||||
elif was_on_floor and !is_on_floor():
|
||||
floor_snap = Vector3.ZERO
|
||||
|
||||
networking.sync_position = position
|
||||
networking.sync_velocity = velocity
|
||||
|
||||
func accelerate(delta : float, p_target_dir : Vector3, p_target_speed : float, p_accel : float):
|
||||
var current_speed : float = velocity.dot(p_target_dir)
|
||||
|
||||
Reference in New Issue
Block a user