【发布时间】:2016-03-10 02:26:53
【问题描述】:
设置:创建我的第一个多人游戏并遇到一个奇怪的问题。 这是一款坦克游戏,玩家可以射击子弹并互相残杀。
您可以为子弹充电以更快/更远地射击。
问题: 当客户端玩家完全充电并释放时,子弹会继续重复生成并且永不停止。如果客户端播放器未完全充电,则不会出现此问题。
我认为问题在于if (m_CurrentLaunchForce >= m_MaxLaunchForce && !m_Fired) 中的更新功能
注意: 主机播放器没有这个问题,因此它与网络有关。
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;
}
【问题讨论】:
-
你从未在第一个电流 > 最大测试中设置
m_Fired = true -
我在
CmdFire()中执行此 if 语句调用,不是吗? -
嗯,m_fired 是局部变量还是类变量?
-
它是一个类变量
-
@MarcB 成功了!但我不明白为什么!
m_fired是一个类变量,它在CmdFire()中被设置为true。
标签: c# networking unity3d unity3d-unet