玩家移动时与子弹碰撞 - Unity UNET
Player colliding with bullet when moving - Unity UNET
设置 : 创建我的第一个多人游戏和 运行 进入一个奇怪的问题。这是一款坦克游戏,玩家可以发射子弹并互相残杀
问题:当客户端在移动时射击,子弹似乎有一点延迟产生导致玩家碰撞用子弹。
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)
注意:宿主机没有这个问题,所以跟网络有关系
如何与客户端同步子弹?
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;
}
设置 : 创建我的第一个多人游戏和 运行 进入一个奇怪的问题。这是一款坦克游戏,玩家可以发射子弹并互相残杀
问题:当客户端在移动时射击,子弹似乎有一点延迟产生导致玩家碰撞用子弹。
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)
注意:宿主机没有这个问题,所以跟网络有关系
如何与客户端同步子弹?
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;
}