Client-authoritative NetworkTransform spawns in wrong position
See original GitHub issueDescription
When spawning a NetworkObject
using SpawnAsPlayerObject
method, it always appear in 0,0,0, ignoring the position it had when it was instantiated.
Reproduce Steps
- Create a simple
ClientNetworkTransform
component that overridesOnIsServerAuthoritative
method to return false - Assign this component to a prefab
- Spawn prefab on server in some place, other than origin
Actual Outcome
Spawned prefab teleports immediately to scene’s origin
Expected Outcome
Spawned prefab should remain the same position it had before spawning over the network.
Screenshots
If applicable, add screenshots to help explain your problem.
Environment
- OS: Windows 11
- Unity Version: 2023.2
- Netcode Version: 1.3.1 (dev branch)
Additional Context
Please take a look at this video.
https://user-images.githubusercontent.com/5504685/234705013-b02f6e37-c108-40d0-bfdd-57b9fecc20e7.mp4
The way I instantiate and spawn the prefab:
public class SceneManager : NetworkBehaviour {
public GameObject playerPrefab;
public Transform spawnPosition;
public void Start() {
NetworkManager.Singleton.OnClientConnectedCallback += OnClientConnected;
}
void OnClientConnected(ulong clientId) {
if (IsHost) {
var newPlayer = Instantiate(playerPrefab, spawnPosition.position + spawnOffset, Quaternion.identity);
newPlayer.GetComponent<NetworkObject>().SpawnAsPlayerObject(clientId);
}
}
}
Thanks a lot!
Issue Analytics
- State:
- Created 5 months ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Spawning as PlayerObject with ClientNetworkTransform
I have a player prefab which has a ClientNetworkTransform component. The server instantiates the player and spawns it as PlayerObject. var ...
Read more >Unity network transform problem - Client spawning objects ...
I'll try and pinpoint the code so I can place it here but I imagine it could be a number of things. Local...
Read more >ClientDriven Sample
In this sample, our spawn points list is server side (to have a single source of truth). ClientNetworkTransforms can be updated by owners...
Read more >Unity - Bullet spawns in wrong position in client
The bullet tends to spawn in a location where the player was a few miliseconds ago, making that the bullet sometimes spawns inside...
Read more >Server Authoritative Movement, Unity Multiplayer with Fish ...
Asset Store: https://assetstore.unity.com/packages/tools/network/fish-net-networking-evolved-207815 Github: ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Got it, now it’s crystal clear! Thanks again for your support! I’ll try this approach!
@Maclay74 That is pretty much correct.
That is pretty much the timing layout. What you can do to have precise “upon spawn” placement (if you want to keep your method of spawning) is the following:
This should allow you to keep pretty much the exact same approach with just the adjustments in who is the owner. In the ClientNetworkTransform, you would need to override the “OnGainedOwnership” and enable the CharacterController locally when that is invoked. You would also need to have the special case for the host as to when you enable the CharacterController…sort of like this:
(did not test the above code…it should work…but if it doesn’t let me know) This way the host only enables the CharacterController once for its local player and for all other client players it leaves them disabled. Then when the host transfers ownership to the client, the client will enable it locally.
Something along these lines would assure the spawn position and rotation is set when spawned and that there is no latency between the client owner, the host, and the other connected clients.