publicoverridevoidOnStartLocalPlayer(){// Get all of the renderers of the tank.MeshRenderer[]renderers=gameObject.GetComponentsInChildren<MeshRenderer>();// Go through all the renderers...for(inti=0;i<renderers.Length;i++){// ... set their material color to the color specific to this tank.renderers[i].material.color=Color.red;}}
[Command]privatevoidCmdFire(){// 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.RigidbodyshellInstance=Instantiate(m_Shell,m_FireTransform.position,m_FireTransform.rotation)asRigidbody;// Set the shell's velocity to the launch force in the fire position's forward direction.shellInstance.velocity=m_CurrentLaunchForce*m_FireTransform.forward;NetworkServer.Spawn(shellInstance.gameObject);// 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;}
voidOnGUI(){// Set the slider's value appropriately.m_Slider.value=m_CurrentHealth;// Interpolate the color of the bar between the choosen colours based on the current percentage of the starting health.m_FillImage.color=Color.Lerp(m_ZeroHealthColor,m_FullHealthColor,m_CurrentHealth/m_StartingHealth);}
2. 玩家状态(网络健康)
打开 _Completed-Assets\Scripts\Tank\TankHealth 脚本
将脚本更改为 NetworkBehaviour
[SyncVar] 指令生命值属性具有 “服务器权限”
将 isServer 检查添加到 TakeDamage 中,所以它只会应用在服务器上
1
2
3
4
5
6
7
8
9
publicclassTankHealth:NetworkBehaviour{//···[SyncVar]privatefloatm_CurrentHealth;// How much health the tank currently has.//···}