1

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

问题:当客户端在移动时射击时,子弹似乎会稍微延迟生成,这会导致玩家与子弹发生碰撞。

问题似乎是玩家本身是本地的,并且子弹正在网络上产生(这导致了延迟)

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

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

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

}
4

2 回答 2

3

您的游戏规则是否需要满足玩家坦克能够自行射击的需求?

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

    Transform bullet = Instantiate(bulletPrefab) as Transform;
    Physics.IgnoreCollision(bullet.GetComponent<Collider>(), GetComponent<Collider>());
于 2016-03-10T00:52:13.317 回答
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; 
}
于 2016-03-10T01:49:13.697 回答