com.unity.netcode.gameobjects
com.unity.netcode.gameobjects copied to clipboard
Setting position of object with NetworkTransform and NetworkRigidbody
Description
I have a player prefab that is spawned by the NetworkManager. The player prefab contains a Rigidbody, a NetworkRigidbody, and a NetworkTransform. I am trying to set the initial position from within "OnNetworkSpawn()" of a custom component that is based on NetworkBehaviour, which is also attached to the player prefab.
What I discovered is that I can only set the position via "Rigidbody.position". I was previously able to set the position via "NetworkTransform.Teleport()". This stopped working either after updating to Unity 2022.1.2 or Netcode for GameObjects 1.0.0-pre.9. I'm not sure which update caused it to break, although I think it's likely that the problem is due to the NGO update.
Actual Outcome
Position can only be set via the Rigidbody component...
GetComponent<Rigidbody>().position = newPosition;
Expected Outcome
The following should work...
transform.position = newPosition;GetComponent<NetworkTransform>.SetState(newPosition, ...);GetComponent<NetworkTransform>.Teleport(newPosition, ...);
Environment
- OS: Windows 11
- Unity Version: 2022.1.2
- Netcode Version: 1.0.0-pre.9
- Netcode Commit: Unknown
I've experimented with this a bit more. It looks like "NetworkTransform.Teleport()" will not work until the "Rigidbody" has been updated on the player game object. I've put together a workaround in my game that involves introducing a delay before teleporting the player to their starting location. After the delay (several seconds, currently set to 2), I set the "Rigidbody" and run "NetworkTransform.Teleport()". This enables consistent starting position updates in both the host and associated clients.
Anyway, there is definitely something wrong with "Teleport()". I don't believe it should be necessary to fiddle around with the "Rigidbody" in order to get it to work.
Note that once the "Rigidbody" has been altered and changes have been communicated to the client, subsequent "Teleport()" calls do work without having to also set the "Rigidbody" position.
Added to backlog to investigate MTT-4264
@lcompton Hello, I was running through the Github issues and noticed that teleporting issues were resolved in PR-2110 which was included in the current version (v1.0.2) of Netcode for GameObjects. If, after you have updated to the most recent version, you are still experiencing issues with NetworkTransform.Teleport, please post them here. 👍
As a side note: You need to set the Rigidbody.isKinematic to true while teleporting with a RigidBody. A good reference script of how this is done can be found here
If you grab the develop branch you can find a working sample of Teleporting here.
Let me know if this works for your project?