在没有玩家输入的情况下重复发射/创建子弹 - UNET Unity

时间:2016-03-09 17:44:13

标签: c# networking unity3d unity3d-unet

设置:创建我的第一个多人游戏并遇到一个奇怪的问题。 这是一个坦克游戏,玩家可以射击子弹并互相杀戮。

你可以给子弹充电,以便更快/更远地射击。

问题: 当客户端玩家完全充电并释放时,子弹继续反复产生并且永不停止。如果客户端播放器未完全充电,则不会发生此问题。

我认为问题是if (m_CurrentLaunchForce >= m_MaxLaunchForce && !m_Fired)

中的更新功能

注意: 主机播放器没有这个问题,因此它与网络有点联系。

private void Update()
{
    if (!isLocalPlayer)
        return;
    // Track the current state of the fire button and make decisions based on the current launch force.
    m_AimSlider.value = m_MinLaunchForce;

    if (m_CurrentLaunchForce >= m_MaxLaunchForce && !m_Fired) {
        m_CurrentLaunchForce = m_MaxLaunchForce;
        CmdFire ();
    } else if (Input.GetButtonDown (m_FireButton) && !m_Fired) {
        m_Fired = false;
        m_CurrentLaunchForce = m_MinLaunchForce;
        m_ShootingAudio.clip = m_ChargingClip;
        m_ShootingAudio.Play();
    } else if (Input.GetButton (m_FireButton)) {
        m_CurrentLaunchForce += m_ChargeSpeed * Time.deltaTime;
        m_AimSlider.value = m_CurrentLaunchForce;
    } else if (Input.GetButtonUp(m_FireButton)) {
        CmdFire ();
    }
}

[Command]
private void CmdFire()
{
    // Set the fired flag so only Fire is only called once.
    m_Fired = true;

    // Create an instance of the shell and store a reference to it's rigidbody.
    GameObject shellInstance = (GameObject)
        Instantiate (m_Shell, m_FireTransform.position, m_FireTransform.rotation) ;

    // Set the shell's velocity to the launch force in the fire position's forward direction.
    shellInstance.GetComponent<Rigidbody>().velocity = m_CurrentLaunchForce * m_FireTransform.forward; 

    // Change the clip to the firing clip and play it.
    m_ShootingAudio.clip = m_FireClip;
    m_ShootingAudio.Play ();
    NetworkServer.Spawn (shellInstance);
    // Reset the launch force.  This is a precaution in case of missing button events.
    m_CurrentLaunchForce = m_MinLaunchForce;

}

1 个答案:

答案 0 :(得分:0)

如果您查看文档,您会发现 - &gt;在与连接关联的播放器对象上调用[Command]函数。这是为了响应&#34; ready&#34;消息,通过将播放器objec传递给NetworkServer.PlayerIsReady()函数。命令调用的参数在网络中是连续的,因此调用服务器函数的值与客户端上的函数值相同。

我认为它不适合你的原因是因为你必须将一个参数传递给函数,如

[Command]
private void CmdFire(bool m_Fired)
{
    // Set the fired flag so only Fire is only called once.
    m_Fired = true;

    // Create an instance of the shell and store a reference to it's rigidbody.
    GameObject shellInstance = (GameObject)
        Instantiate (m_Shell, m_FireTransform.position, m_FireTransform.rotation) ;

    // Set the shell's velocity to the launch force in the fire position's forward direction.
    shellInstance.GetComponent<Rigidbody>().velocity = m_CurrentLaunchForce * m_FireTransform.forward; 

    // Change the clip to the firing clip and play it.
    m_ShootingAudio.clip = m_FireClip;
    m_ShootingAudio.Play ();
    NetworkServer.Spawn (shellInstance);
    // Reset the launch force.  This is a precaution in case of missing button events.
    m_CurrentLaunchForce = m_MinLaunchForce;

}

然后像这样称呼它:

CmdFire(m_Fired)其中m_Fired必须指向玩家自己的变量。