Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用于客户端的Unity3D网络拍摄循环_C#_Unity3d_Multiplayer - Fatal编程技术网

C# 用于客户端的Unity3D网络拍摄循环

C# 用于客户端的Unity3D网络拍摄循环,c#,unity3d,multiplayer,C#,Unity3d,Multiplayer,我想再造坦克!统一教程系列中的多人游戏。我也看了网络系列节目。但我在实施正确的射击方面遇到了问题。坦克可以充电以增加子弹的发射力。它适用于主机,但客户端在启动时陷入困境 守则: [ClientCallback] private void Update() { if (!isLocalPlayer) return; if (m_CurrentLaunchForce >= MaxLaunchForce && !m_Fired)

我想再造坦克!统一教程系列中的多人游戏。我也看了网络系列节目。但我在实施正确的射击方面遇到了问题。坦克可以充电以增加子弹的发射力。它适用于主机,但客户端在启动时陷入困境

守则:

[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。

我自己找到了解决方案。我实现了另一个函数“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;
然后告诉服务器使用诸如力、位置、旋转等选项生成对象作为函数参数来验证它们,这取决于您对作弊的担心程度