[英]Bullets being fired / created repeatedly without player's input - UNET Unity
Setting: Creating my first multiplayer game and running into an odd issue. 场景:创建我的第一个多人游戏并遇到一个奇怪的问题。 it's a tank game where players can shoot bullets and kill each other. 这是一款坦克游戏,玩家可以射击子弹并互相杀死。
You can charge the bullets to shoot it faster/further away. 您可以充电子弹以使其更快/更远地射击。
Problem: When the client player charges fully and releases, the bullet continue to be spawned repeatedly and never stops. 问题:客户端播放器充满电并释放后,子弹将继续重复产生,并且永不停止。 This issue doesn't occur if the client player does Not charges fully. 如果客户端播放器未完全充电,则不会发生此问题。
I believe the issue is the update function within the if (m_CurrentLaunchForce >= m_MaxLaunchForce && !m_Fired)
我相信问题是if (m_CurrentLaunchForce >= m_MaxLaunchForce && !m_Fired)
的更新功能
Note: The host player does not have this problem, therefore it's somehow related to networking. 注意:主机播放器没有此问题,因此与网络有某种关系。
private void Update()
{
if (!isLocalPlayer)
return;
// Track the current state of the fire button and make decisions based on the current launch force.
m_AimSlider.value = m_MinLaunchForce;
if (m_CurrentLaunchForce >= m_MaxLaunchForce && !m_Fired) {
m_CurrentLaunchForce = m_MaxLaunchForce;
CmdFire ();
} else if (Input.GetButtonDown (m_FireButton) && !m_Fired) {
m_Fired = false;
m_CurrentLaunchForce = m_MinLaunchForce;
m_ShootingAudio.clip = m_ChargingClip;
m_ShootingAudio.Play();
} else if (Input.GetButton (m_FireButton)) {
m_CurrentLaunchForce += m_ChargeSpeed * Time.deltaTime;
m_AimSlider.value = m_CurrentLaunchForce;
} else if (Input.GetButtonUp(m_FireButton)) {
CmdFire ();
}
}
[Command]
private void CmdFire()
{
// Set the fired flag so only Fire is only called once.
m_Fired = true;
// Create an instance of the shell and store a reference to it's rigidbody.
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;
// Change the clip to the firing clip and play it.
m_ShootingAudio.clip = m_FireClip;
m_ShootingAudio.Play ();
NetworkServer.Spawn (shellInstance);
// Reset the launch force. This is a precaution in case of missing button events.
m_CurrentLaunchForce = m_MinLaunchForce;
}
If you look trough the documentation you will find this -> [Command] functions are invoked on the player object associated with a connection. 如果仔细阅读文档,您会发现-> [Command]函数在与连接关联的播放器对象上被调用。 This is setup in response to the "ready" message, by passing the player objec to the NetworkServer.PlayerIsReady() function. 通过将播放器objec传递给NetworkServer.PlayerIsReady()函数来响应“就绪”消息进行设置。 The arguments to the command call are seriialized across the network, so that the server function is invoked with the same values as the function on the client. 通过网络对命令调用的参数进行序列化,以便使用与客户端上的函数相同的值来调用服务器函数。
I think the reason why it wasnt working for you is because you have to pass an argument to the function like 我认为它对您不起作用的原因是因为您必须将参数传递给类似
[Command]
private void CmdFire(bool m_Fired)
{
// Set the fired flag so only Fire is only called once.
m_Fired = true;
// Create an instance of the shell and store a reference to it's rigidbody.
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;
// Change the clip to the firing clip and play it.
m_ShootingAudio.clip = m_FireClip;
m_ShootingAudio.Play ();
NetworkServer.Spawn (shellInstance);
// Reset the launch force. This is a precaution in case of missing button events.
m_CurrentLaunchForce = m_MinLaunchForce;
}
And then call it like this: 然后像这样调用它:
CmdFire(m_Fired) where m_Fired has to point to the players own variable. CmdFire(m_Fired),其中m_Fired必须指向玩家自己的变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.