简体   繁体   English

Unity 中的多人游戏

[英]Multiplayer in Unity

Below is the code for movement of player object and it works perfectly fine.下面是播放器 object 的移动代码,它运行良好。 But in the TellClientsToMoveClientRpc why are we doing transform.position as it refers to the the current gameobject but not every object moves, only the one which called the above Rpc moves which is desirable.但是在TellClientsToMoveClientRpc中我们为什么要进行transform.position因为它指的是当前的游戏对象,而不是每个object移动,只有调用上述 Rpc 移动的那个才是可取的。 But why all other gameobject do not move?但是为什么所有其他游戏对象都不动呢?

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;
        }
    }
}```

A multiplayer game like this means that there are multiple clients running the same program (the game), meaning that there is a copy of all of the same game objects on each client's instance of the game .像这样的多人游戏意味着有多个客户端运行相同的程序(游戏),这意味着每个客户端的游戏实例上都有所有相同游戏对象的副本

This means that the game object you attached your HelloWorldPlayer script on is present on each client.这意味着您附加HelloWorldPlayer脚本的游戏 object 存在于每个客户端上。

When you call TellServerToMoveClientServerRpc from a client, it will execute that function on the same game object the client called the function from , but on the server.当您从客户端调用TellServerToMoveClientServerRpc时,它将在同一个游戏 object 上执行 function 客户端从服务器调用 function ,但This is because you added a [ServerRPC] .这是因为您添加了[ServerRPC]

Now that the server has control over what's going to happen next, it calls TellClientsToMoveClientRpc .现在服务器已经控制了接下来会发生什么,它调用TellClientsToMoveClientRpc Now this time, since you added a [ClientRpc] , that function will be called on the same game object the original client called the function from , but now on every single client that's connected to your server.现在这一次,由于您添加了[ClientRpc] ,因此 function 将在同一个游戏 object 上调用,原来的客户端称为 function 从您的服务器连接到的每个客户端,但是现在。

Since it's always the same game object calling the function (just on different instances of the game, running on different computers), not all game objects move: only the one that initially called the TellServerToMoveClientServerRpc function.由于它始终是同一个游戏 object 调用 function (只是在不同的游戏实例上,在不同的计算机上运行),并非所有游戏对象都会移动:只有最初调用TellServerToMoveClientServerRpc ZC1C42542068E6AB5394D11 的游戏对象会移动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM