Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Deal damage + random int and display inflicted damage

Discussion in 'Scripting' started by quayle, Mar 1, 2015.

  1. quayle

    quayle

    Joined:
    Jun 5, 2014
    Posts:
    14
    I'm a rookie and I need some help to figure this out:

    I have a public variable damage:

    Code (CSharp):
    1.     public int damage;
    and this function:

    Code (CSharp):
    1.     public void GetHit (int playerDamage)
    2.     {
    3.         health = health - playerDamage;
    4.     }
    I call the function with the damage parameter like this:

    Code (CSharp):
    1.     player.GetHit(damage);
    I want the total damage inflicted to be damage AND then in addition a random variable, between say 1 and 5.

    I also want to print the inflicted damage each time the player has been hit.

    I understand I can use Random.Range(1,5) and Display.Log() but I don't see how I can implement it.
     
  2. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    838
    Hey, you seemed to have explained your problem pretty well but I'm not sure what you mean by not knowing how to implement it. Wouldn't this work?

    Code (csharp):
    1.  
    2. public void GetHit (int playerDamage)
    3.     {
    4.         int totalDamage = playerDamage + Random.Range(1,5);
    5.      
    6.         Debug.Log(totalDamage);
    7.      
    8.         health = health - totalDamage;
    9.     }
     
  3. quayle

    quayle

    Joined:
    Jun 5, 2014
    Posts:
    14
    Yes that's excellent, thanks!