Munchy2007

Unet5Tut7Pt1HealthAndDamageFull

Mar 3rd, 2016
4,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.09 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.Networking;
  4.  
  5. public class HealthAndDamage : NetworkBehaviour {
  6.     [SyncVar(hook = "OnHealthChanged")]
  7.     public int health;
  8.     [SyncVar]
  9.     Vector3 respawnPos;
  10.     Camera playerCam;
  11.  
  12.     public override void OnStartLocalPlayer ()
  13.     {
  14.         CmdSetHealth(100);
  15.         playerCam = GetComponent<NetworkPlayer>().playerCam;
  16.     }
  17.  
  18.     [Command]
  19.     void CmdSetHealth(int newHealth)
  20.     {
  21.         health = newHealth;
  22.     }
  23.  
  24.     [Server]
  25.     public void TakeDamage(GameObject fromPlayer)
  26.     {
  27.         health = Mathf.Max(health - 10, 0);
  28.  
  29.         if(health <= 0)
  30.         {
  31.             DoDeath();
  32.             fromPlayer.GetComponent<PlayerShoot>().AddScore();
  33.         }
  34.     }
  35.  
  36.     [Server]
  37.     void DoDeath()
  38.     {
  39.         respawnPos = GetRandomSpawnPoint();
  40.         RpcHandlePlayerDeath();
  41.     }
  42.  
  43.     [Server]
  44.     Vector3 GetRandomSpawnPoint()
  45.     {
  46.         var spawnPoints = NetworkManager.singleton.startPositions;
  47.         return spawnPoints[Random.Range(0,spawnPoints.Count)].position;
  48.     }
  49.        
  50.     [ClientRpc]
  51.     void RpcHandlePlayerDeath()
  52.     {
  53.         StartCoroutine(HandlePlayerDeath());
  54.     }
  55.  
  56.     [Client]
  57.     IEnumerator HandlePlayerDeath()
  58.     {
  59.         setVisibleState(false);
  60.         if(isLocalPlayer)
  61.         {
  62.             playerCam.transform.SetParent(null, true);
  63.             transform.position = respawnPos;
  64.         }
  65.         yield return new WaitForSeconds(2f);
  66.         Respawn();
  67.     }
  68.  
  69.     [Client]
  70.     void Respawn()
  71.     {
  72.         if(isLocalPlayer)
  73.         {
  74.             playerCam.transform.SetParent(transform);
  75.             playerCam.transform.localPosition = new Vector3(0,10,0);
  76.             CmdSetHealth(100);
  77.         }
  78.         setVisibleState(true);
  79.  
  80.     }
  81.  
  82.     [Client]
  83.     void setVisibleState(bool state)
  84.     {
  85.         GetComponent<Renderer>().enabled = state;
  86.         transform.Find("LabelHolder").gameObject.SetActive(state);
  87.     }
  88.  
  89.     void OnHealthChanged(int newHealth)
  90.     {
  91.         health = newHealth;
  92.  
  93.         if(health < 100)
  94.         {
  95.             StartCoroutine(ShowHitEffect());
  96.         }
  97.  
  98.         if(isLocalPlayer)
  99.         {
  100.             HUD.instance.DisplayHealth(health);
  101.         }
  102.     }
  103.  
  104.     IEnumerator ShowHitEffect()
  105.     {
  106.         var material = GetComponent<Renderer>().material;
  107.         Color savedColor = material.color;
  108.         material.color = Color.yellow;
  109.         yield return new WaitForSeconds(0.4f);
  110.         material.color = savedColor;
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment