Multiplayer Cannot See Other Players

Godot Version

4.2.2

Question

Players can’t see one another.

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”

const SPEED = 5.0
const SPRINT_SPEED = 7.5
const JUMP_VELOCITY = 10.0

Get the gravity from the project settings to be synced with RigidBody nodes.

var gravity = 30.0

func _enter_tree():
set_multiplayer_authority(str(name).to_int())

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)
if Input.is_action_pressed(“shoot”):
if anim_player.current_animation != “shoot”:
play_shoot_effects()

func play_shoot_effects():
if not is_multiplayer_authority(): return
anim_player.stop()
anim_player.play(“shoot”)
muzzel_flash.restart()
muzzel_flash.emitting = true

func _physics_process(delta):
if not is_multiplayer_authority(): return
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * delta

# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
	velocity.y = JUMP_VELOCITY

# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
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 == "shoot":
	pass
elif input_dir != Vector2.ZERO and is_on_floor():
	anim_player.play("move")
else:
	anim_player.play("idle")

move_and_slide()

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 = {}

func _ready():
pass

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():
$CanvasLayer.hide()

enet_peer.create_server(PORT)
multiplayer.multiplayer_peer = enet_peer
multiplayer.peer_connected.connect(add_player)

add_player(multiplayer.get_unique_id())

func _on_join_button_pressed():
$CanvasLayer.hide()

enet_peer.create_client("172.0.0.1", PORT)
multiplayer.multiplayer_peer = enet_peer

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

func delete_player(peer_id):
var player = get_node(str(peer_id))
if player != null:
player.queue_free()
players.erase(str(peer_id))

Camera will not be set for players

Add not to the if statement.