Spawning Player in Multiplayer Environment

How to instantiate the player and Sync Animation and Transfrom in muliplayer.

Original Post on Discord

by user 406778568597831690

Good start would be the docs :cactus:

There’s a sample being created to explain this usecase more easily.

In the mean time, the docs are a good start to exaplin the basics of networking.

And then there are the Player State and SyncTransform. You can see the usage here: https://github.com/needle-tools/needle-engine-samples/blob/sample/networking/package/Runtime/Networking/Player/Scripts/NetworkingPlayer~/Player.ts

To make the SyncTransform work, you need to call the requestOwnership() on it, but only on a instance that is yours (the local instance).

That’s where IsOwner boolean comes in, that is calculated from PlayerState like this:

   isOwner(): boolean { 
        if(!this.playerState || !this.playerState.owner)
            return false;

        return this.playerState.owner == this.context.connection.connectionId;
    }

You can condition the inputs as well, so the synced players are not moving with your controls and only your local instance is being moved.

Lastly, there is the PlayerSync, where you specify what the player prefab is. Ten when ever a player connects it spawns this prefab.

So at the end it’s quite simple :slight_smile:

You can copy this folder and try it out yourself.
https://github.com/needle-tools/needle-engine-samples/tree/sample/networking/package/Runtime/Networking

.
.
Don’t be afraid to ask if anything isn’t clear. Like, what is a prefab or if some of my wording isn’t clear :slight_smile:

I had tried these components earlier, without the ownership it didn’t work is it like if we add these components without the ownwership it will not instanciate ??

by user 406778568597831690

PlayerSync

  • instantiates the player on all devices upon user joing
  • if you are a new player in the room, this spawns the other players that are already there.
  • Concet is that you do not have any player prefabs in your scene, you only reference a prefab that is spawned for every player (both remote & local)

PlayerState

  • ID of the spawned player
  • With this you can decide if this spawned player is your local one or if it is a remote player being synced.

SyncTransform

  • Has to manually activated by requestOwnership() but only on a instance that is YOU / the local player, not the remotes ones. (That’s why you need to condition the call with isOwner func- in the Player.ts)

So to answer your question:

No, syncTransform doesn’t instantiate anything. Regardless of the requestOwnership() call.

The PlayerSync is the one that spawns/instantiates the player.

so the Player orefab inspector is like this,

by user 406778568597831690

by user 406778568597831690

and player component is like this,
image.png

by user 406778568597831690

i have modified the player script, made sure whatever code is there gets translated

by user 406778568597831690

Ok, that seems to be correct :+1: What issues do you currently have?

player is not moving

by user 406778568597831690

Can you post the playermovement script

this is my script
message.txt

by user 406778568597831690

I see, if you commend out the is Owner check and the return above it, does it work then?

no stil not working i added a log to check whether its getting instantiated, its getting instantiated but movement is not working

by user 406778568597831690

in logs its throwing a warning that its unable to find the instantiated object or parent

by user 406778568597831690