客户端的Unity3D网络拍摄循环

时间:2017-04-17 11:15:41

标签: c# unity3d multiplayer

我想重建坦克!来自Unity教程系列的游戏作为多人游戏。我也已经看过网络系列了。但我在实施拍摄权方面遇到了问题。坦克可以充电以增加子弹的发射力。它适用于主机,但客户端陷入困境。

代码:

[ClientCallback]
 private void Update()
 {
     if (!isLocalPlayer)
         return;

     if (m_CurrentLaunchForce >= MaxLaunchForce && !m_Fired)
     {
         m_CurrentLaunchForce = MaxLaunchForce;
         Debug.Log("Max force achieved! Firing!");
         CmdFire();
     }
     else if (Input.GetButtonDown("Fire1"))
     {
         m_Fired = false;
         m_CurrentLaunchForce = MinLaunchForce;
         Debug.Log("Start charging up!");
     }
     else if (Input.GetButton("Fire1") && !m_Fired)
     {
         m_CurrentLaunchForce += m_ChargeSpeed * Time.deltaTime;
         Debug.Log("Charging up!");
     }
     else if (Input.GetButtonUp("Fire1") && !m_Fired)
     {
         Debug.Log("Firing with low force!");
         CmdFire();
     }
 }


[Command]
 private void CmdFire()
 {
     m_Fired = true;
     Rigidbody shellInstance = Instantiate(ShellPrefab, FireTransform.position, FireTransform.rotation);
     shellInstance.velocity = m_CurrentLaunchForce * transform.forward;
     m_CurrentLaunchForce = MinLaunchForce;
     NetworkServer.Spawn(shellInstance.gameObject);
     m_Fired = false;
 }

不是主机的客户端陷入第二种情况:

if (m_CurrentLaunchForce >= MaxLaunchForce && !m_Fired)

我检查了调试器中的变量,而currentLaunchForce从未重置为minLaunchForce。

1 个答案:

答案 0 :(得分:0)

我自己找到了解决方案。我实现了另一个函数“fire()”,它首先从Update函数调用,然后再次调用命令“cmdfire()”。在fire命令中我重置变量,在命令中我告诉服务器以force为参数为alle客户端生成射弹。

private void fire()
{
    m_Fired = true;
    CmdFire(m_CurrentLaunchForce);
    m_CurrentLaunchForce = MinLaunchForce;
    m_Fired = false;
    startReloadTime();
}


private void CmdFire(float launchForce)
    {
        Rigidbody bulletInstance = Instantiate(BulletPrefab, FireTransform.position, FireTransform.rotation);
        bulletInstance.velocity = launchForce * transform.forward;
        NetworkServer.Spawn(bulletInstance.gameObject);
    }

似乎如果您使用某些变量来检查客户端上的某些条件,您还必须自己在客户端上重置它们,就像我的情况一样:

private float m_CurrentLaunchForce;                                             
private bool m_Fired;

然后告诉服务器使用诸如力,位置,旋转等选项来生成对象以验证它们,具体取决于你对作弊的关注程度。