Help With Multiplayer: Players that Join can see anything or walk

Godot Version

4.5 Beta 4

Question

I am trying to Implement multiplayer into my relativly empty project but for some reason the player that joins doesnt get a camera assigned and cant walk/move their camera, I followid this tutorial: https://www.youtube.com/watch?v=G28bNlgICJg but it doesn’t seem to work properly.
I am using a MultiplayerSpawner and MultiplayerSynchronizer

Any help is Appreciated

Player Script
extends CharacterBody3D

# Variables
var Health: float = 100.00
const Speed: float = 4
const CrouchSpeed: float = 2
const SprintSpeed: float = 8
var CurrentSpeed = Speed
const JUMP_VELOCITY = 4.5
var mouseCap = true
var stamina: float = 100.00
var look_dir: Vector2
@onready var camera: Camera3D = $Camera
var camera_sens = 50

func _enter_tree() -> void:
	set_multiplayer_authority(name.to_int())
	$Camera.current = is_multiplayer_authority()

# check for multiplayer authority when adding multiplayer
func _physics_process(delta: float) -> void:
	if is_multiplayer_authority():
		if $Camera/RayCast.is_colliding(): $UI/Crosshair.visible = true # show crosshair on interactable object
		else: $UI/Crosshair.visible = false # hide it if none
		
		if Input.is_action_just_pressed("jump") and is_on_floor(): velocity.y = JUMP_VELOCITY # handle jump
		
		# handle movement
		var input_dir := Input.get_vector("left", "right", "up", "down")
		var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
		if direction:
			velocity.x = direction.x * CurrentSpeed
			velocity.z = direction.z * CurrentSpeed
		else:
			velocity.x = move_toward(velocity.x, 0, CurrentSpeed)
			velocity.z = move_toward(velocity.z, 0, CurrentSpeed)
		
		if Input.is_action_just_pressed("quit"): # Capture or Show the mouse on pressing ESC
			mouseCap = !mouseCap
			if mouseCap: Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
			else: Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
		
		# update metrics (progress bars)
		$UI/VBoxContainer/HBoxContainer/Stamina.value = stamina
		$UI/VBoxContainer/HBoxContainer/Health.value = Health
		
	if not is_on_floor(): velocity += get_gravity() * delta # add gravity
	
	move_and_slide()
	rotate_camera(delta)

# update look direction
func _input(event: InputEvent) -> void: if event is InputEventMouseMotion: look_dir = event.relative * 0.01

# camera rotation function
func rotate_camera(delta: float, sens_mod: float = 1.0):
	var input = Input.get_vector("look_left", "look_right", "look_down", "look_up")
	look_dir += input
	rotation.y -= look_dir.x * camera_sens * delta
	camera.rotation.x = clamp(camera.rotation.x - look_dir.y * camera_sens * sens_mod * delta, -1.5, 1.5)
	look_dir = Vector2.ZERO

Level Script with host/join functionality
extends Node3D

var peer = ENetMultiplayerPeer.new()
@export var playerSCN : PackedScene
@onready var lobbyCamera: Camera3D = $LobbyCamera

func _on_host_pressed() -> void:
	# Create a server and add the host
	peer.create_server(2643)
	multiplayer.multiplayer_peer = peer
	multiplayer.peer_connected.connect(add_player)
	add_player()
	lobbyCamera.current = false

func _on_join_pressed() -> void:
	peer.create_client("127.0.0.1", 2643) # Change preset IP to taking a Line Edit later
	multiplayer.multiplayer_peer = peer
	lobbyCamera.current = false

func _on_quit_pressed() -> void: get_tree().quit() # Quit the app

func add_player(id = 1):
	# Instanciate and add the player
	var player = playerSCN.instantiate()
	player.name = str(id)
	call_deferred("add_child", player)

func exit(id):
	# Call del_player()
	multiplayer.peer_disconnected.connect(del_player)
	del_player(id)

func del_player(id):
	# tell the server to delete a player
	rpc("_del_player", id)

# Delete the player on found
@rpc("any_peer", "call_local") func _del_player(id):
	get_node(str(id)).queue_free()

Update: I fixed it I just had a issue with the path when adding it as a child, I should have added it in the same place as my multiplayer spawner spawn location mb

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.