【发布时间】:2022-01-15 10:27:35
【问题描述】:
下面是播放器对象移动的代码,它工作得很好。但是在TellClientsToMoveClientRpc 为什么我们要这样做transform.position 因为它指的是当前的游戏对象 但并不是每个对象都在移动,只有调用上述Rpc 的那个才是可取的。但是为什么所有其他游戏对象都不动呢?
using UnityEngine;
namespace HelloWorld
{
public class HelloWorldPlayer : NetworkBehaviour
{
private const float speed = 20f;
private void Update()
{
if (!IsOwner)
return;
PlayerMove();
}
void PlayerMove()
{
Vector3 velocity = new(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
if(velocity.sqrMagnitude >= 1)
{
TellServerToMoveClientServerRpc(velocity, NetworkManager.Singleton.LocalClientId);
}
}
[ServerRpc]
void TellServerToMoveClientServerRpc(Vector3 velocity, ulong clientId)
{
TellClientsToMoveClientRpc(velocity, clientId);
}
[ClientRpc]
void TellClientsToMoveClientRpc(Vector3 velocity, ulong clientId)
{
transform.position += speed * Time.deltaTime * velocity;
}
}
}```
【问题讨论】:
标签: c# unity3d multiplayer