Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Recieve XP Based on Damage

Discussion in 'Scripting' started by Wilelle, Mar 26, 2016.

  1. Wilelle

    Wilelle

    Joined:
    Jun 16, 2015
    Posts:
    93
    I got an enemy with a set mount of XP that it will yield upon death. But since in the game I'm working on the player will have NPCs targeting the same enemies, I want to divide the XP between everyone involved in the battle based on how much damage they dealt. So if the player damages the enemy for 40% of its HP and a few NPCs damages the enemy for the other 60%, he player only receives 40% of the XP.

    However, I have no idea how to even start with this. Percentage is pretty much nonexistent in Unity as far as I know, at least in this form, and also comparing two different values... Yeah, I was just wondering if anyone knows a good solution for this or if I'm demanding too much.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    add a dictionary to the thing being killed, have it log the "attacker" (key) and the "damage dealt" (value) to the dictionary so you can keep a track of who did what.

    When the thing is killed you should know the total (i.e. the starting health) and can do a quick iterate to calc the ratio (value / total), percentage just being a "100 base" ratio, and send an event/message/whatever to each attacker with the appropriate xp value.
     
    Wilelle and Ryiah like this.
  3. Wilelle

    Wilelle

    Joined:
    Jun 16, 2015
    Posts:
    93
    Thanks! I was a little confused by parts of your explanation, but it put my on the right track. I made sure every attacker up to 5 different ones at once are stored within the enemy, and upon death it uses the equation "(attacker #'s damage * XP) / max health". Now it works wonders!