Search Unity

Trying to make a UI element that shows the amount of dashes the play has

Discussion in 'Scripting' started by Robster95, Sep 15, 2020.

  1. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154
    I'm trying to make a part of my players UI that shows how many dashes he will have left. The player has a max of 3 dashes.

    I am confused and dont know where to start with making the UI show the dashes and update depending on the amount of dashes the player has left.

    here is my code for the player dashes

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. public class PlayerDash : MonoBehaviour
    4. {
    5.     //probably should put dash in its own script so I dont put to much in one script... but who knows.
    6.     [Header("Dash")]
    7.     public float dashTime;
    8.     [SerializeField]
    9.     private float dashDist;
    10.     public float dashRadius;
    11.     private bool canDash = true;
    12.     public bool dashPressed = false;
    13.     private Vector3 targetPosition;
    14.     private Vector3 mousepos;
    15.     private int maxDashCount = 3;
    16.     [SerializeField]
    17.     private int currentDashCount;
    18.     [SerializeField]
    19.     private float maxTimer;
    20.     [SerializeField]
    21.     private float currentTimer;
    22.     private TrailRenderer trail;
    23.     private void Start()
    24.     {
    25.         currentDashCount = maxDashCount;
    26.         currentTimer = maxTimer;
    27.         trail = GetComponent<TrailRenderer>();
    28.     }
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.         //sets up the mouse to be in relation to what the camera see's
    33.         mousepos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    34.         //dash radius
    35.         dashDist = Vector2.Distance(transform.position, mousepos);
    36.         Vector3 mousePosition = new Vector3(mousepos.x, mousepos.y, 0);
    37.         Vector3 direction = mousePosition - transform.position;
    38.         //if requirements met, starts dash code
    39.         if (currentDashCount > 0)
    40.         {
    41.             if (dashDist <= dashRadius)
    42.             {
    43.                 if (Input.GetKeyDown(KeyCode.LeftShift) && canDash == true)
    44.                 {
    45.                     targetPosition = new Vector3(mousepos.x, mousepos.y, 0);
    46.                     dashPressed = true;
    47.                     canDash = false;
    48.                 }
    49.                 //calls for dash movement code
    50.                 if (dashPressed == true)
    51.                 {
    52.                     Playerdash();
    53.                 }
    54.             }
    55.             else if (dashDist > dashRadius)
    56.             {
    57.                 if (Input.GetKeyDown(KeyCode.LeftShift) && canDash == true)
    58.                 {
    59.                     targetPosition = transform.position + direction.normalized * dashRadius;
    60.                     dashPressed = true;
    61.                     canDash = false;
    62.                 }
    63.                 //calls for dash movement code
    64.                 if (dashPressed == true)
    65.                 {
    66.                     Playerdash();
    67.                 }
    68.             }
    69.         }
    70.         DashTimer();
    71.     }
    72.     //Dash movement code
    73.     public void Playerdash()
    74.     {
    75.         trail.enabled = true;
    76.         Physics2D.IgnoreLayerCollision(10, 11);
    77.         transform.position = Vector3.MoveTowards(transform.position, targetPosition, dashTime * Time.deltaTime);
    78.         if (transform.position == targetPosition)
    79.         {
    80.             dashPressed = false;
    81.             canDash = true;
    82.             currentDashCount = currentDashCount - 1;
    83.             Physics2D.IgnoreLayerCollision(10, 11, false);
    84.             trail.enabled = false;
    85.         }
    86.     }
    87.     private void DashTimer()
    88.     {
    89.         if(currentDashCount < maxDashCount)
    90.         {
    91.             if(currentTimer > 0)
    92.             {
    93.                 currentTimer -=  1 * Time.deltaTime;
    94.                 if(currentTimer <= 0)
    95.                 {
    96.                     currentDashCount = currentDashCount + 1;
    97.                     currentTimer = maxTimer;
    98.                 }
    99.             }
    100.         }
    101.     }
    102.     private void OnDrawGizmos()
    103.     {
    104.         Gizmos.DrawWireSphere(transform.position, dashRadius);
    105.     }
    106. }
    107.  
    I want to have a separate script that holds the information for the UI.

    If anyone can help me that will be great. I want there to be 3 icons that are enabled that show according to how many are available with the currentdashcount and I want to try to make a timer to show the refresh time when they're not available.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Make a horizontal layout group with your three tickmarks, then drag those ticmarks into a script that calls .SetActive() on the ones you want.

    The horizontal layout group will cause them to bunch together nicely.

    If you look in the HealthMeter scene in my Datasacks project, you can see how I did it with a very generic system, but you don't need to put all that in your project: just do the steps above.

    Datasacks is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/datasacks

    https://github.com/kurtdekker/datasacks

    https://gitlab.com/kurtdekker/datasacks

    https://sourceforge.net/projects/datasacks/
     
    Robster95 likes this.
  3. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154
    Ok i'll look into this. Not going to lie this problem kind of made me discouraged from coding so i'll try it again in a little!