Search Unity

XP Bar w/ Smooth Animation for uMMORPG

Discussion in 'Scripting' started by blackbrosg, Dec 13, 2019.

  1. blackbrosg

    blackbrosg

    Joined:
    Apr 9, 2018
    Posts:
    20
    hello people I'm going crazy to give a smooth animation to the xp bar slider from ummorpg, any ideas?, thanks x/

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public partial class UIExperienceBar : MonoBehaviour {
    5.     public GameObject panel;
    6.     public Slider slider;
    7.     public Text statusText;
    8.  
    9.     void Update() {
    10.         var player = Utils.ClientLocalPlayer();
    11.         panel.SetActive(player != null); // hide while not in the game world
    12.         if (!player) return;
    13.  
    14.         slider.value = player.ExperiencePercent();
    15.         statusText.text = "Lvl." + player.level + " (" + (player.ExperiencePercent() * 100).ToString("F0") + "%)";
    16.  
    17.         // addon system hooks
    18.         Utils.InvokeMany(typeof(UIExperienceBar), this, "Update_");
    19.     }
    20. }
    21.  
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Code (CSharp):
    1. public float Health;
    2. public float MaxHealth;
    3. public GameObject FillerImage;
    4.  
    5. private void Update() {
    6.    FillerImage.localScale = Vector3.Lerp(filler.localScale, new Vector3(Health/MaxHealth, FillerImage.localScale.y, FillerImage.localScale.z), .1f);
    7. }
     
  3. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    Code (CSharp):
    1. slider.value =  Mathf.MoveTowards(slider.value, player.ExperiencePercent(), 10f * Time.deltaTime);
    Replace 10f with something more suited to how fast you want it to go.
     
  4. blackbrosg

    blackbrosg

    Joined:
    Apr 9, 2018
    Posts:
    20
    sorry doesnt work :(
     
  5. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    I'm assuming there's a single callback whenever experience is gained.

    You can do it this way. Store the value that is currently displayed locally, and the target value(actual server-side experience value), inside of a class you create to animate the slider.

    You can use coroutines in the following fashion: Whenever the player gains experience, you can interpolate from the start value (displayed value at the moment where experience was gained) to the new value (exp value stored on server).

    The reason you want to store the displayed value is because you want to be able to pick up from exactly where the XP gauge was whenever new experience is gained.

    Something along those lines
    Code (CSharp):
    1. private float displayedValue;
    2. private float targetValue;
    3.  
    4. private void OnXPGained()
    5. {
    6.     // targetValue = whatever you're doing to get the player XP's server-side value
    7.     StopAllCoroutines();
    8.     StartCoroutine(UpdateXPGaugeRoutine());
    9. }
    10.  
    11. private IEnumerator UpdateXPGaugeRoutine()
    12. {
    13.     float startValue = displayedValue;
    14.     float timeStamp = Time.time;
    15.     float duration = 0.3f; // You could also make the duration dynamic based on how much xp has to be covered.
    16.     while (Time.time < timeStamp + duration)
    17.     {
    18.         float t = (Time.time - timeStamp) / duration;
    19.         t = Mathf.Sin(Mathf.PI * t * 0.5f); // Can smooth t for a non linear effect
    20.         displayedValue = Mathf.Lerp(startValue, targetValue, t);
    21.         // Convert value to slider format, whatever makes sense and then pass it to slider
    22.         yield return null;
    23.     }
    24.     displayedValue = targetValue;
    25.     // Convert and assign once again
    26. }
     
  6. blackbrosg

    blackbrosg

    Joined:
    Apr 9, 2018
    Posts:
    20
    WORRRKSSSSS thank uuuuu!
     
  7. blackbrosg

    blackbrosg

    Joined:
    Apr 9, 2018
    Posts:
    20
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public partial class UIExperienceBar : MonoBehaviour {
    5.     public GameObject panel;
    6.     public Slider slider;
    7.     public Text statusText;
    8.  
    9.     void Update() {
    10.         var player = Utils.ClientLocalPlayer();
    11.         panel.SetActive(player != null); // hide while not in the game world
    12.         if (!player) return;
    13.  
    14.         slider.value =  Mathf.MoveTowards(slider.value, player.ExperiencePercent(), 10f * Time.deltaTime);
    15.         statusText.text = "Lvl." + player.level + " (" + (player.ExperiencePercent() * 100).ToString("F0") + "%)";
    16.  
    17.         // addon system hooks
    18.         Utils.InvokeMany(typeof(UIExperienceBar), this, "Update_");
    19.     }
    20. }
    21.  
    now i need when the experience is 100% play a level up sound, anybodys help me?