UNITY | PROJECT | RPG Tutorial |11. Enemy & Inflicting Damage

 

3

4
“InflictDamage” Script
  • create Variables
    • damageAmount for Damage Amount for this weapon
    • allowedRange for Reach of this weapon
  • in Update() method,
    • when Left Mouse Button is pressed,
      • create a RayCastHit variable
      • put the RayCast in the variable
      • when the Distance obtained by RayCasting(hit.distance) is lower than the Reach(allowedRange),
        • hit.transform is in this context, the enemy spider
        • Enemy spider has the function, DeductPoints
        • make the funtion attached to Enemy GameObject activated by using SendMessage activated
          • InflictDamage will send a message to SpiderEnemy and activate the function
        • since we don’t want to put a receiver for the message in the spider object, set the option, DontRequireReceiver
5
Creating Dummy for Enemy
6
Adding “InflictDamage” Script to Weapon
7
“SpiderEnemy”
  • create a variable for the Spider’s Health
  • create a function, DeductPoint with argument, int damageAmount
    • deduct the damage from the health when this function is activated
  • in Update() method,
    • if the health is below than 0,
      • get the coroutine started
  • create a IEnumerator, DeathSpider()
    • wait for 0.5 sec before destroying the gameObject
7-1
Inflicting Damage Working