移动时玩家与子弹相撞 - Unity UNET

时间:2016-03-09 22:47:21

标签: c# networking unity3d unity3d-unet

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

问题:当客户端在移动时拍摄时,子弹似乎会产生一点延迟,导致玩家与子弹发生碰撞。

  

问题似乎是玩家本身就是本地和子弹   正在网络上产生(导致延迟)

注意:主播播放器没有此问题,因此它与网络有关。

如何将子弹与客户端播放器同步?

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

}

2 个答案:

答案 0 :(得分:3)

您的游戏规则是否需要迎合玩家的坦克才能自行投射?

如果没有,一个简单的解决方案就是让射弹的对手在它被激活时忽略它的所有者的对手:

    Transform bullet = Instantiate(bulletPrefab) as Transform;
    Physics.IgnoreCollision(bullet.GetComponent<Collider>(), GetComponent<Collider>());

答案 1 :(得分:0)

我通过以下方式解决了这个问题。如果有人可以查看确认我的答案,那就太好了。

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