簡體   English   中英

移動時球員與子彈相撞 - Unity UNET

[英]Player colliding with bullet when moving - Unity 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);

}

你的游戲規則是否需要迎合玩家的坦克能夠射擊自己?

如果沒有,一個簡單的解決方案是讓射彈的對撞機在被激活時忽略它的所有者的對撞機:

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

我通過以下方式解決了它。 如果有人可以查看確認我的答案,那就太好了。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM