Unity-Technologies / com.unity.netcode.gameobjects

Netcode for GameObjects is a high-level netcode SDK that provides networking capabilities to GameObject/MonoBehaviour workflows within Unity and sits on top of underlying transport layer.
MIT License
2.12k stars 430 forks source link

AnticipatedNetworkTransform breaks InLocalSpace configuration. #2972

Open leozzyzheng opened 1 month ago

leozzyzheng commented 1 month ago

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

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.

NoelStephensUnity commented 1 month ago

@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.

leozzyzheng commented 1 month ago

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