Search Unity

Resolved Stupidest Unity Question Ever to deal more damage

Discussion in '2D' started by XsvGamer, Jun 30, 2020.

  1. XsvGamer

    XsvGamer

    Joined:
    Apr 9, 2020
    Posts:
    20
    Hi guys,

    I know this might just be a really stupid question, I have this script and would like it to deal double the damage, I thought I could call out the dealdamage script and just add a *2 at the end of the PlayerHealthController.instance.DealDamage - this doesn't work.

    here is the code which is set on a proximity mine which explodes when player gets close, need it to cause double the usual damage...

    private void OnTriggerEnter2D(Collider2D other)
    {
    if (other.tag == "Player")
    {
    Destroy(gameObject);
    AudioManager.instance.PlaySFX(28);
    Instantiate(explosion, transform.position, transform.rotation);
    PlayerHealthController.instance.DealDamage();

    AudioManager.instance.PlaySFX(26);
    }
    }

    I also understand that this which is in the PlayerHealthController script: currentHealth--; (removes one bit of health from the player but could I just change the script here or add another function in order to deal extra damage here instead... by maybe amending the currentHealth--; kerfuffled...??

    Sorry I'm still learning all this...
     
  2. CatalinMurariu

    CatalinMurariu

    Joined:
    Mar 17, 2020
    Posts:
    3
    Depending on what your DealDamage method does, the easiest way would be to just call it twice:
    Code (CSharp):
    1. PlayerHealthController.instance.DealDamage();
    2. PlayerHealthController.instance.DealDamage();

    This wouldn't be a good idea if that method also handles some kind of animation of the player character in reaction to getting hurt. If it does, the alternative is to modify your method to accept a multiplier like this:
    Code (CSharp):
    1. public void DealDamage(float multiplier = 1f){
    2.   normalDamage = 1; //where normalDamage would be 1 I guess in your case
    3.   currentHealth -= normalDamage * multiplier;
    4. }

    The default value of the parameter still allows it to be called without any arguments, and it will still do the old thing. And when you want to do double damage you call it like this:
    Code (CSharp):
    1. PlayerHealthController.instance.DealDamage(2);
    You can also scrap the whole multiplier thing, and call it actual damage - and do the value that you pass in, instead of multiplying it with your normal damage value. Hope that's clear enough, apologies if it sounds convoluted.
     
  3. XsvGamer

    XsvGamer

    Joined:
    Apr 9, 2020
    Posts:
    20
    Wow @CatalinMurariu thanks for that. I understand what you've done here, will go and apply it now and see if I can get it working. Thanks man! :) Will let you know
     
  4. XsvGamer

    XsvGamer

    Joined:
    Apr 9, 2020
    Posts:
    20
    perfect mate, works a charm! Thanks for your help @Catalinmu
     
  5. CatalinMurariu

    CatalinMurariu

    Joined:
    Mar 17, 2020
    Posts:
    3
    I'm glad it helped you!
     
    XsvGamer likes this.