1

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

您可以为子弹充电以更快/更远地射击。

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

我认为问题在于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;

}
4

1 回答 1

0

如果您查看文档,您会发现 this -> [Command] 函数在与连接关联的播放器对象上调用。这是通过将播放器对象传递给 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 必须指向玩家自己的变量。

于 2016-03-09T18:26:53.090 回答