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

Problem : When the client shoots while moving, the bullet seems to spawn with a little delay which causes the the player to collide with the bullet.

The issue seems to be that the player itself is local and the bullet is being spawned on the network ( which is causing the delay)

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

How can I sync the bullet with the client player?

private void Fire(){
    // Set the fired flag so only Fire is only called once.
    m_Fired = true;
    CmdCreateBullets ();
    // Change the clip to the firing clip and play it.
    m_ShootingAudio.clip = m_FireClip;
    m_ShootingAudio.Play ();
    // Reset the launch force.  This is a precaution in case of missing button events.
    m_CurrentLaunchForce = m_MinLaunchForce;
}

[Command]
private void CmdCreateBullets()
{

    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; 

    NetworkServer.Spawn (shellInstance);

}
Rana
  • 1,675
  • 3
  • 25
  • 51
  • If the projectile's being created server-side, it should always be ahead of the player (in the timeline), yet the issue is caused by the player's collider intersecting the model by the time the player receives the projectile's position info right? Is this because you are performing hit collision detection client-side? – Happy Apple Mar 10 '16 at 00:53
  • That's exactly right and Hit collision detection is being done client-side – Rana Mar 10 '16 at 00:57

2 Answers2

3

Do your game rules need to cater for a player's tank being able to shoot itself?

If not, a simple solution is to have the projectile's collider ignore it's owner's collider when it's being activated:

    Transform bullet = Instantiate(bulletPrefab) as Transform;
    Physics.IgnoreCollision(bullet.GetComponent<Collider>(), GetComponent<Collider>());
Happy Apple
  • 726
  • 4
  • 13
  • I guess this is one way of solving the issue. however, I would like the player to receive damage if they stand in front of a wall and shoot. The bullet explosion radius in this case will damage the player. +1 for a last resort solution, thanks. – Rana Mar 10 '16 at 01:01
0

I solved it the following way. if someone can look over to confirm my answer, it would be nice.

private void Fire(){
    // Set the fired flag so only Fire is only called once.
    m_Fired = true;
    CmdCreateBullets ();
    // Change the clip to the firing clip and play it.
    m_ShootingAudio.clip = m_FireClip;
    m_ShootingAudio.Play ();
    // Reset the launch force.  This is a precaution in case of missing button events.
    m_CurrentLaunchForce = m_MinLaunchForce;
}

[Command]
private void CmdCreateBullets()
{
    RpclocalBullet ();
}

[ClientRpc]
private void RpclocalBullet(){
    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 = 25f * m_FireTransform.forward; 
}
Rana
  • 1,675
  • 3
  • 25
  • 51