当前位置:   article > 正文

Unity之NetCode多人网络游戏联机对战教程(10)--玩家动画同步_unitynetcode 象棋联机

unitynetcode 象棋联机

前言

这次的动画同步与位置同步,可以说实现思路是一样的,代码相似度也非常高

NetworkAnimation

如果直接挂载这个脚本只有Host(服务端)才可以同步,Client是没有权限同步的。


服务端权威

直接修改之前的PlayerSync.cs脚本。

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");
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89

客户端权威

写个OwnerNetworkAnimator.cs挂载到角色身上即可

using Unity.Netcode.Components;

public class OwnerNetworkAnimator : NetworkAnimator
{
    protected override bool OnIsServerAuthoritative()
    {
        return false;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

脚本的Animator直接把角色拖进去即可读取。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/116471
推荐阅读
相关标签
  

闽ICP备14008679号