Advertisement
Munchy2007

Unet5Tut5Pt1HealthAndDamageV1

Feb 24th, 2016
4,604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.82 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.  
  9.     public override void OnStartLocalPlayer ()
  10.     {
  11.         CmdSetHealth(100);
  12.     }
  13.  
  14.     [Command]
  15.     void CmdSetHealth(int newHealth)
  16.     {
  17.         health = newHealth;
  18.     }
  19.  
  20.     [Server]
  21.     public void TakeDamage(GameObject fromPlayer)
  22.     {
  23.         health = Mathf.Max(health - 10, 0);
  24.     }
  25.  
  26.  
  27.     void OnHealthChanged(int newHealth)
  28.     {
  29.         health = newHealth;
  30.  
  31.         if(health < 100)
  32.         {
  33.             StartCoroutine(ShowHitEffect());
  34.         }
  35.     }
  36.        
  37.     IEnumerator ShowHitEffect()
  38.     {
  39.         var material = GetComponent<Renderer>().material;
  40.         Color savedColor = material.color;
  41.         material.color = Color.yellow;
  42.         yield return new WaitForSeconds(0.4f);
  43.         material.color = savedColor;
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement