Search Unity

Reflecting damage to the attacking class.

Discussion in 'Scripting' started by foremano17, Apr 11, 2021.

  1. foremano17

    foremano17

    Joined:
    Feb 19, 2021
    Posts:
    2
    Hi,

    I am making status effects for a game i've been working on. One of those status effects (ThickSkin) makes the attacker take damage whenever you're hit.

    I've included an image of one of my hit methods (one class would call another's hit method to deal damage, each attackable class has it's own hit method). One of the peramiters is 'attacker' this is where i hoped to pass in the class that is calling the method so i could reflect damage back to it but there are multiple classes that can hit the player so i can't give it just one data type (for example i can't make it the Enemy data type because then i couldn't be able to reflect damage to the EnemyTwo class).

    I've hilighted the spot where i would call the Hit() method on the attacking class. Any help would be useful,
    Thanks.
    Screenshot (91).png
     
  2. The-Peaceful

    The-Peaceful

    Joined:
    Aug 11, 2018
    Posts:
    5
    I'd normally go and make a seperate class on the enemy one for the AI and one for the stats of the enemy. For example, you have the EnemyOne and EnemyTwo (for movement and hitting the player) which holds a reference to its EnemyStats component (which has a method TakeDamage(float damage)). So that way you could just pass in the EnemyStats component as a parameter in your Hit() method and call EnemyStats.TakeDamage() when necessary.
     
  3. foremano17

    foremano17

    Joined:
    Feb 19, 2021
    Posts:
    2
    Thanks, took a while to fix all the errors but it works now.