【问题标题】:Player colliding with bullet when moving - Unity UNET玩家移动时与子弹碰撞 - Unity UNET
【发布时间】:2016-06-24 13:45:55
【问题描述】:

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

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

问题似乎是播放器本身是本地的,而子弹 正在网络上生成(导致延迟)

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

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

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

}

【问题讨论】:

  • 如果射弹是在服务器端创建的,它应该总是在玩家之前(在时间线中),但问题是由于玩家的对撞机在玩家收到弹丸的位置信息对吗?这是因为您在客户端执行碰撞检测吗?
  • 完全正确,Hit 碰撞检测正在客户端完成

标签: c# networking unity3d unity3d-unet


【解决方案1】:

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

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

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

【讨论】:

  • 我想这是解决问题的一种方法。但是,我希望玩家站在墙前射击时会受到伤害。这种情况下的子弹爆炸半径会对玩家造成伤害。 +1 最后的解决方案,谢谢。
【解决方案2】:

我通过以下方式解决了它。如果有人能过目确认我的回答,那就太好了。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 2018-12-31
    • 1970-01-01
    相关资源
    最近更新 更多