1

Setting: Creating my first multiplayer game and running into an odd issue. it's a tank game where players can shoot bullets and kill each other.

You can charge the bullets to shoot it faster/further away.

Problem: When the client player charges fully and releases, the bullet continue to be spawned repeatedly and never stops. This issue doesn't occur if the client player does Not charges fully.

I believe the issue is the update function within the if (m_CurrentLaunchForce >= m_MaxLaunchForce && !m_Fired)

Note: The host player does not have this problem, therefore it's somehow related to networking.

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;

}
Rana
  • 1,675
  • 3
  • 25
  • 51

1 Answers1

0

If you look trough the documentation you will find this -> [Command] functions are invoked on the player object associated with a connection. This is setup in response to the "ready" message, by passing the player objec to the NetworkServer.PlayerIsReady() function. The arguments to the command call are seriialized across the network, so that the server function is invoked with the same values as the function on the client.

I think the reason why it wasnt working for you is because you have to pass an argument to the function like

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

}

And then call it like this:

CmdFire(m_Fired) where m_Fired has to point to the players own variable.

BNg
  • 25
  • 6
  • Thanks for trying but this didn't help me – Rana Mar 09 '16 at 18:45
  • No problem. Another suggestion to look at is your m_Fired variable. From what you posted I don't see where you set m_fired to false again. Try setting it to false at the end of your command function. – BNg Mar 10 '16 at 15:42