Godot Version
4.2.2
Question
Hi I’m developing a multiplayer FPS game and I’m traveling good so far but there is a small issue. The synchronization between peers work but they aren’t seeing the host’s position being updated. The host can see them just fine and the players can see each other. Hope this is a simple issue and I’m just too dumb to figure it out. Cheers.
World Code
extends Node3D
@onready var main_menu = $CanvasLayer/MainMenu
@onready var address_entry = $CanvasLayer/MainMenu/MarginContainer/VBoxContainer/AddressEntry
const Player = preload(“res://scene/world/player.tscn”)
const PORT = 25555
var enet_peer = ENetMultiplayerPeer.new()
var players = {}
var on_main_menu = false
var on_loading_screen = true
var timer = Timer.new()
func _ready():
add_child(timer)
timer.wait_time = 10.0
on_main_menu = true
if on_main_menu == true:
$Control3.hide()
$Snare_March.play()
func _unhandled_input(event):
if Input.is_action_just_pressed(“exit”):
delete_player(multiplayer.get_unique_id())
get_tree().quit()
func _on_host_button_pressed():
$Control/Control3/CanvasLayer.hide()
enet_peer.create_server(PORT)
multiplayer.multiplayer_peer = enet_peer
multiplayer.peer_connected.connect(add_player)
on_main_menu = false
$Snare_March.stop()
$Button_Click.play()
add_player(multiplayer.get_unique_id())
func _on_join_button_pressed():
$Control/Control3/CanvasLayer.hide()
enet_peer.create_client("192.168.0.146", PORT)
multiplayer.multiplayer_peer = enet_peer
multiplayer.peer_connected.connect(add_player)
on_main_menu = false
$Snare_March.stop()
$Button_Click.play()
add_player(multiplayer.get_unique_id())
func add_player(peer_id):
var player = Player.instantiate()
player.name = str(peer_id)
add_child(player)
players[player.name] = player # Store the player reference in the dictionary
print(player.name)
player.set_multiplayer_authority(peer_id)
func delete_player(peer_id):
var player = get_node(str(peer_id))
if player != null:
player.queue_free()
players.erase(str(peer_id))
Player Code
extends CharacterBody3D
@onready var camera = $Camera3D
@onready var anim_player = $AnimationPlayer
@onready var muzzel_flash = $Camera3D/Node3D/MuzzelFlash
@onready var host_button = $“…/CanvasLayer/MainMenu/MarginContainer/VBoxContainer/HostButton”
@onready var join_button = $“…/CanvasLayer/MainMenu/MarginContainer/VBoxContainer/JoinButton”
@onready var ak_shot_sound_effect = $ak_shot
const SPEED = 5.0
const SPRINT_SPEED = 7.5
const JUMP_VELOCITY = 10.0
var gravity = 30.0
var character_has_zoomed_in = false
func _enter_tree():
set_multiplayer_authority(multiplayer.get_unique_id())
func _ready():
if is_multiplayer_authority(): return
camera.current = true
func _input(event):
if not is_multiplayer_authority(): return
if event is InputEventMouseMotion:
self.rotate_y(-event.relative.x * 0.001)
camera.rotate_x(-event.relative.y * 0.001)
camera.rotation.x = clamp(camera.rotation.x, -PI/2, PI/2)
func play_shoot_effects():
if not is_multiplayer_authority(): return
anim_player.stop()
ak_shot_sound_effect.play()
anim_player.play(“shoot”)
muzzel_flash.restart()
muzzel_flash.emitting = true
func _physics_process(delta):
if not is_multiplayer_authority(): return
if not is_on_floor():
velocity.y -= gravity * delta
if character_has_zoomed_in == false:
if Input.is_action_pressed(“shoot”):
if anim_player.current_animation != “shoot” and anim_player.current_animation != “zoomed_in”:
play_shoot_effects()
if Input.is_action_pressed("zoom"):
if character_has_zoomed_in == false:
if anim_player.current_animation != "zoom_in": #if the player has pressed RMB and the animation has not started then zoom in
character_has_zoomed_in = true # Set this to true here
anim_player.stop()
anim_player.play("zoom_in")
if Input.is_action_pressed("zoom"):
if anim_player.current_animation != "zoom_in":
if character_has_zoomed_in == true:
anim_player.stop()
anim_player.play("zoomed_in")
$Crosshair.hide()
if Input.is_action_just_released("zoom"): # Check if the zoom action is released
if character_has_zoomed_in == true:
anim_player.stop()
anim_player.play("zoom_out")
character_has_zoomed_in = false # Set this to false here
$Crosshair.show()
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
var input_dir = Input.get_vector("left", "right", "forward", "backward")
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
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)
if Input.is_action_pressed("alt"):
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
else:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
if Input.is_action_pressed("sprint"):
velocity.x = direction.x * SPRINT_SPEED
velocity.z = direction.z * SPRINT_SPEED
if anim_player.current_animation != "zoom_in" and anim_player.current_animation != "shoot" and anim_player.current_animation != "zoomed_in" and anim_player.current_animation != "zoom_out":
if input_dir != Vector2.ZERO and is_on_floor():
anim_player.play("move")
else:
anim_player.play("idle")
move_and_slide()