Unity's NetCode multiplayer online game online battle tutorial (10)--player animation synchronization

Preface

This time, the animation synchronization and position synchronization can be said to be implemented in the same way, and the code similarity is also very high.

NetworkAnimation

If you mount this script directly, only the Host (server) can synchronize, and the Client does not have permission to synchronize.


Server authority

Modify the previousPlayerSync.cs script directly.

using Unity.Netcode;
using UnityEngine;

public class PlayerSync : NetworkBehaviour
{
    
    
    private NetworkVariable<Vector3> _syncPos = new();
    private NetworkVariable<Quaternion> _syncRota = new();
    private Transform _syncTransform;
    private NetworkVariable<bool> _syncisIdle = new();
    private NetworkVariable<bool> _syncisRun = new();
    private Animator _syncAnimator;

    public void SetTarget(Transform player)
    {
    
    
        _syncTransform = player;
        _syncAnimator = player.GetComponent<Animator>();
    }

    private void Update()
    {
    
    
        if (IsLocalPlayer)
        {
    
    
            UploadTransform();
            UploadAnimation();
        }
    }

    private void FixedUpdate()
    {
    
    
        if (!IsLocalPlayer)
        {
    
    
            SyncTransform();
            SyncAnimation();
        }
    }

    private void SyncTransform()
    {
    
    
        _syncTransform.position = _syncPos.Value;
        _syncTransform.rotation = _syncRota.Value;
    }

    private void SyncAnimation()
    {
    
    
        _syncAnimator.SetBool("isIdle", _syncisIdle.Value);
        _syncAnimator.SetBool("isRun", _syncisRun.Value);
    }

    private void UploadTransform()
    {
    
    
        if (IsServer)
        {
    
    
            _syncPos.Value = _syncTransform.position;
            _syncRota.Value = _syncTransform.rotation;
        }
        else
        {
    
    
            UploadTransformServerRpc(_syncTransform.position, _syncTransform.rotation);
        }
    }

    [ServerRpc]
    private void UploadTransformServerRpc(Vector3 position, Quaternion rotation)
    {
    
    
        _syncPos.Value = position;
        _syncRota.Value = rotation;
    }

    private void UploadAnimation()
    {
    
    
        if (IsServer)
        {
    
    
            _syncisIdle.Value = _syncAnimator.GetBool("isIdle");
            _syncisRun.Value = _syncAnimator.GetBool("isRun");
        }
        else
        {
    
    
            UploadAnimationServerRpc();
        }
    }

    [ServerRpc]
    private void UploadAnimationServerRpc()
    {
    
    
        _syncisIdle.Value = _syncAnimator.GetBool("isIdle");
        _syncisRun.Value = _syncAnimator.GetBool("isRun");
    }
}


client authority

Write one OwnerNetworkAnimator.cs and mount it on the character

using Unity.Netcode.Components;

public class OwnerNetworkAnimator : NetworkAnimator
{
    
    
    protected override bool OnIsServerAuthoritative()
    {
    
    
        return false;
    }
}

ScriptAnimatorDrag the character directly to read it.

Guess you like

Origin blog.csdn.net/a924282761/article/details/134424996
Recommended