处理合作社游戏物理(角色移动,命中…)

问题描述 投票:0回答:1

我已经在网上搜索了一段时间以寻找解决问题的方法,并且找不到足够的资源来帮助我。

[我正在使用UDP库https://github.com/RevenantX/LiteNetLib在Unity 3D中开发合作RPG游戏,并且设法建立了客户端与服务器的连接,并在其中发送了玩家运动的矢量(不应用时间计算,这样服务器就可以自行处理它),并计算出角色应该位于的新位置,然后我向所有玩家广播我发送给服务器的相同矢量,以便他们也可以计算出物理学。

我遇到的问题是运动似乎很滞后,有时计算错误。

我不确定这是由于我的本地PlayerController脚本还是由于不良的网络或不良的设计,我实际上应该在其中发送客户端的新绝对位置。

[我知道这是一个很难回答的问题,我希望尝试自己创建多人游戏的开发人员有一些准则或专业知识,

我还应该注意,作弊与我无关,因为这是一种合作游戏,而不是竞争性游戏。

提前感谢您的帮助。

这是我的本地PlayerController代码的片段:

void Update()
        {
            // Get Input for axis
            float h = Input.GetAxis("Horizontal");
            float v = Input.GetAxis("Vertical");

            // Calculate the forward vector
            Vector3 camForward_Dir = Vector3.Scale(UnityEngine.Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized;
            Vector3 move = v * camForward_Dir + h * UnityEngine.Camera.main.transform.right;

            if (move.magnitude > 1f) move.Normalize();

            // Calculate the rotation for the player
            move = transform.InverseTransformDirection(move);

            // Get Euler angles
            float turnAmount = Mathf.Atan2(move.x, move.z);

            transform.Rotate(0, turnAmount * RotationSpeed * Time.deltaTime, 0);

            if (_characterController.isGrounded)
            {
                _moveDir = transform.forward * move.magnitude;
                _moveDir *= Speed;
            }
            _moveDir.y -= Gravity * Time.deltaTime;

            var delta = _moveDir * Time.deltaTime;
            _characterController.Move(delta);

            if (!IsMoving())
            {
                //_wasMoving = false;
                return;
            }
            // we send the move direction so that the server and other client`s can calculate for themselves
            OnPlayerMoved?.Invoke(_moveDir, Time.deltaTime);
        }
unity3d networking physics multiplayer
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.