Trying a multiplayer game with characters with is own camera

Hi, I’m trying a multiplayer game *using *:

a) A Networking empty object with SyncedRoom: Networking(Script)

b) A player prefab, this prefab has a model, its own character controller script (as a child), a camera (and this camera has the component SyncedCamera) and the component PlayerState.

c) Two empty objects both with the component PlayerSinc and the Asset Field of this component has the prefab described in b).

What’s happening?
When I open two browser windows and try to move one of the characters, I move both. How can I have a independent control in both windows?

Attach my Networking object :

Thank you all, you have been people who help me understand this great new engine.

Original Post on Discord

by user 632418299711324161

Hey :wave: you have to condition your input.

You have your local player that you wish to control, but you also have the remote player, with the same scripts. These scripts ask for input and thus move the instance around.

As a reference you can use the simple player networking sample that is currently in making:
https://github.com/needle-tools/needle-engine-samples/tree/samples/networking/package/Runtime/Networking/Player
Where the important script is this one: https://github.com/needle-tools/needle-engine-samples/blob/samples/networking/package/Runtime/Networking/Player/Scripts/NetworkingPlayer~/Player.ts

The is owner function is the important bit. ||(This code works, but is bit outdated, the PlayerState has a isLocalPlayer() function that has the same implementation, just so you know)||

—

Also you don’t meantion SyncedTransform, see the implementation above on how to use it. The position of the player is not synchronized without calling the requestOwnership(); on the local player.

—

Let me know if you would get stuck with anything or if something isn’t clear :cactus:

How can I try the player networking sample that is in github, I mean. How Can I open it to see in Unity Editor ?

by user 632418299711324161

Simplest is to clone the repo:
git clone https://github.com/needle-tools/needle-engine-samples

checkout the netwokring branch:
git checkout samples/networking

And then add the samples package to your unity (that will also replace the one downlaoded before, if you did that)
By opening the package manager, clicking the + → add from disk and locate the package.json that is in the downloaded samples at package/package.json

I have a question. I have been checking the Networking example, with the SynCamera object which has the Synced Camera script with its Camera Prefab = CameraPrefab (a object that has not a camera, it has the body and eye model). Is this object which"Instantiate" a new player with its own camera when another browser window is open ?

by user 632418299711324161

Thank you. :slightly_smiling_face:

by user 632418299711324161

Ah, had to get familiar with the SyncedCamera since i haven’t used it before.

It implements all networking into itself and it is a self sufficient system. It doesn’t use PlayerSync or PlayerState.
That’s why the PlayerState.isLocalPlayer will always return false, because the script needs to be told who is the owner. That is what PlayerSync does.

So i would advice to use PlayerSync to spawn your player upon joining and then your controller should reference your PlayerState on your instanced player and condition the input with isLocalPlayer. And do not use SyncedCamera for this usecase.

Also, then your position and rotation won’t be updated, so you need to add the SyncedTransform as well and request the ownership.

.
.
What exactly is your player? How do you visualize it? and in what environment does it move?

Okey. I’m doing a third person controller game. The player can move around a terrain with planes and 3D primitives objects . He moves and jumps.

Player(Funtional Cube) has :

  • Rigibody
  • BoxCollider
  • ThirdPersonController homemade script

The importat children are the LookAt which holds the Camera in order to generate the third person perspective. And the second is the PJ, now is just a Cube but It can be replace to be any 3D model.
image.png

by user 632418299711324161

This is how it looks :

by user 632418299711324161

My goal is have two players in that circuit, instantiating the same player prefab Player(Funtiona Cube).

by user 632418299711324161

But I think that this is the importat part tha you mentioned “So i would advice to use PlayerSync to spawn your player upon joining and then your controller should reference your PlayerState on your instanced player and condition the input with isLocalPlayer”

by user 632418299711324161

Exactly, from what i understand i think you should:

  • remove the SyncedCamera component
  • remove all character prefabs from the scene if there are any
  • add PlayerSync to an empty object in your scene and drop the player prefeb there
  • add PlayerState to the top object of the player prefab.
  • Reference the PlayerState on the main script on the PJ object
  • disable itself / ignroe input
  • disable rigidbody

The PlayerSync is another empty gameObject that just reference the player prefab ?

by user 632418299711324161

yes, it spawns the prefabs in the scene.

Oh nice nice. And What do you mean by “disable itself / ignore input” ? You mean disable PlayerState reference in my main script ?

by user 632418299711324161

No no.

If there are multipl epalyers on the screen, that means there are multiple ThirdPersonController and rigidbody instances in our application.

We od not want to control those and we do not want to calculate physics for remote players, just for our local player.

So, by “disable itself” i mena to call this.enabled = false; in ThirPersonController to stop the update method to be called.

Alterantive way to still have some logic running is to add if statements through out your code to define what runs on remote players and what on local ones

image.png