所以我正在尝试用游戏对象的统一网络代码做一个在线游戏,但客户端动画不起作用
主机端动画效果完美(主机端和客户端都可以看到主机的动画) 我还记录了动画师中使用的速度值,但对于客户端它始终为 0
(Playermovement 部分有效,所以由 navmeshagent 速度计算的速度变量怎么可能对于客户端是 0 而对于主机不是 0?)
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.AI;
public class PlayerNetwork : NetworkBehaviour
{
public NavMeshAgent agent;
public float rotateSpeedMovement = 0.05f;
private float rotateVelocity;
public GameObject animGameObject;
public Animator anim;
float motionSmoothTime = 0.1f;
private void Start()
{
anim = animGameObject.GetComponent<Animator>();
agent = GetComponent<NavMeshAgent>();
}
void Update()
{
if (!IsOwner) return;
if (Input.GetMouseButtonDown(1))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
MoveServerRPC(ray);
}
float speed = agent.velocity.magnitude / agent.speed;
Debug.Log(speed);
AnimationServerRPC(speed);
}
[ServerRpc]
private void MoveServerRPC(Ray ray) // this part works
{
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
if(hit.collider.tag =="Ground")
{
//movement
agent.SetDestination(hit.point);
agent.stoppingDistance = 0;
//rotation
Quaternion rotationToLookAt = Quaternion.LookRotation(hit.point - transform.position);
float rotationY = Mathf.SmoothDampAngle(transform.eulerAngles.y,
rotationToLookAt.eulerAngles.y,
ref rotateVelocity,
rotateSpeedMovement * (Time.deltaTime * 5));
transform.eulerAngles = new Vector3(0, rotationY, 0);
}
}
}
[ServerRpc]
private void AnimationServerRPC(float speed)
{
anim.SetFloat("Speed",speed, motionSmoothTime, Time.deltaTime);
}
}