Search Unity

Question Help with script math

Discussion in 'Scripting' started by EdoC-QWERTY, Jan 2, 2022.

  1. EdoC-QWERTY

    EdoC-QWERTY

    Joined:
    Feb 1, 2020
    Posts:
    75
    Hello,

    I need some help with a script.
    In my game I have objects that can be damaged.

    To show the damage taken, I have different sprites that I want to swap each time the object get hit and become more and more damaged.

    The thing is that I have different types of Destructible objects with different number of sprites to show each damaged state.

    I created a list to be able to add different number of damaged sprites to each object.

    I need help to create a function ("SpriteUpdate" in the script below) that changes the sprite based on the current hp / max HP of the object taking into consideration how many sprites are in the list.

    For example if an object has only one sprite, the sprite will be changed when the hp are less than 50% of the max hp.

    If an object has 3 sprites and 100 max HP each sprite changes when the current hp are less than 99%, 66% and 33% of max hp.

    I would like to avoid some super long function with a lot of if/then/else blocks to check the current hp.

    Thank you


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Linq;
    5. public class Object : Destructible
    6. {
    7.     // Start is called before the first frame update
    8.  
    9.     public List<Sprite> damagedsprite = new List<Sprite>();
    10.  
    11.     public override void OnSpawn()
    12.     {
    13.         hp = maxhp;
    14.     }
    15.  
    16.  
    17.     public  override void OnDestroyed()
    18.     {
    19.         if(OnDestroyPrefab!=null)
    20.         {
    21.             GameObject ondestroyGO = Instantiate(OnDestroyPrefab,transform.position,Quaternion.identity);
    22.         }
    23.      
    24.         SpawnDrops();
    25.         Destroy(gameObject);
    26.     }
    27.  
    28.  
    29.     public  override void OnDamaged()
    30.     {
    31.         SpriteUpdate();
    32.     }
    33.  
    34.     public void SpriteUpdate()
    35.     {
    36.        int  spritenumber = damagedsprite.Count;
    37.        int maxHP = maxhp;
    38.        int currentHP = hp;
    39.  
    40.        //  
    41.        //   ???????????
    42.       //
    43.  
    44.     }
    45. }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    This will get you a linear distribution:
    Code (csharp):
    1.  
    2. float spritenumber = (float)damagedsprite.Count;
    3. float maxHP = (float)maxhp;
    4. float currentHP = (float)hp;
    5.  
    6. int index = Mathf.FloorToInt((currentHP / maxHP) * (spritenumber + 1f));
    7.  
    8. if(index < damagedsprite.Count)
    9.    // set the sprite to damagedsprite[index]
    10.  
    Which is what you want for 1 sprite, and probably 2 sprites (66% and 33%), but will change sprites at 25%,50%,75% for 3 sprites whereas you seem to want 33%, 66%, 99% so you might need to make that a special case.
     
  3. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    Math problem is okay —-


    I have X amounts of sprites for any given object, how to decide when to display those sprites?

    The Frame to +1 at damage is a X / (HP / 100)

    problem here is one list might contain 7, another list might contain 3. Best way to do it is to track your units HP in a percentage instead of a value. And so at least you can measure 100% I should be at frame 0, (int)33.3% i should be frame 1, (int)66% frame 2. Etc

    So for if your percentHP < (100 / X.Count) * i = then update frame to int of value (100 / X.Count) * i. Will take some debugging for sure.
     
  4. EdoC-QWERTY

    EdoC-QWERTY

    Joined:
    Feb 1, 2020
    Posts:
    75
    Thank you both for the help.

    I'm gonna test this now.