com.unity.netcode.gameobjects icon indicating copy to clipboard operation
com.unity.netcode.gameobjects copied to clipboard

AnticipatedNetworkTransform breaks InLocalSpace configuration.

Open leozzyzheng opened this issue 1 year ago • 2 comments

Description

AnticipatedNetworkTransform breaks InLocalSpace configuration.

Reproduce Steps

  1. Add AnticipatedNetworkTransform component to any GameObject with NetworkObject.
  2. Enable InLocalSpace in inspector inside AnticipatedNetworkTransform component.
  3. Start a NetworkManager in Host mode.
  4. Spawn this NetworkObject by any method.
  5. Check inspector, you can find InLocalSpace in inspector become unchecked.

Actual Outcome

InLocalSpace of AnticipatedNetworkTransform is reset to false and transform is synced in world space.

Expected Outcome

InLocalSpace should keep checked in inspector and transform should be synced by local space not the world space.

Screenshots

No need.

Environment

  • OS: Windows 11
  • Unity Version: 2022.3.10f1
  • Netcode Version: 1.9.1
  • Netcode Commit: https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/commit/7d27c5141123e3b9f31419ccca7768a640a45d56

Additional Context

Here is the source code of how AnticipatedNetworkTransform override the OnNetworkSpawn:

    public override void OnNetworkSpawn()
    {
        base.OnNetworkSpawn();
        m_OutstandingAuthorityChange = true;
        ApplyAuthoritativeState();
        ResetAnticipatedState();

        m_AnticipatedObject = new AnticipatedObject { Transform = this };
        NetworkManager.AnticipationSystem.RegisterForAnticipationEvents(m_AnticipatedObject);
        NetworkManager.AnticipationSystem.AllAnticipatedObjects.Add(m_AnticipatedObject);
    }

The line call ApplyAuthoritativeState() will use the initial data of m_LocalAuthoritativeNetworkState to override the local configuration like InLocalSpace, which is false by default. Please check whether it's reasonable to call ApplyAuthoritativeState in OnNetworkSpawn.

Also found those code doesn't check InLocalSpace and directly set the world space position:

    public void AnticipateMove(Vector3 newPosition)
    {
        ......
        transform.position = newPosition;
        ......
    }


    public void AnticipateState(TransformState newState)
    {
        ......
        var transform_ = transform;
        transform_.position = newState.Position;
        transform_.rotation = newState.Rotation;
        transform_.localScale = newState.Scale;
        ......
    }

Rotation has the same problem, please review the code of whole class, there are many other places has the same issue.

leozzyzheng avatar Jul 16 '24 06:07 leozzyzheng

@leozzyzheng The AnticipatedNetworkTransform component is a basic example of how to implement an anticipated transform. We will look into expanding this example to include other features of NetworkTransform in the future.

NoelStephensUnity avatar Jul 22 '24 15:07 NoelStephensUnity

@NoelStephensUnity Understand, so I think it might be better to add some documents or comments for this component to make it more clear.

leozzyzheng avatar Jul 24 '24 03:07 leozzyzheng