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

fillAmount on triger to full than instantly unfill during period of time (GUI)

Discussion in 'Scripting' started by alenmusic, Jun 30, 2015.

  1. alenmusic

    alenmusic

    Joined:
    Apr 7, 2015
    Posts:
    5
    Hi, hope you guys can help me from banging head to the wall at the moment :D
    What I want to achieve is fill Image to full on pickup of something than that fill to instantly start "unfill" during 5 seconds.
    What this code does is it fills the image than wait for 5 seconds and than unfill itself during 5 seconds period of time. :confused:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class PowerUpsController : MonoBehaviour {
    7.  
    8.     public List<Image> speedSprites;
    9.     public bool SpeedCooldown;
    10.     public float WaitTime=5.0f;
    11.  
    12.  
    13.     public Player player;
    14.  
    15.     // Use this for initialization
    16.     void Start ()
    17.     {
    18.         player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
    19.         SpeedCooldown = false;
    20.     }
    21.  
    22.     public void UpdatePowerUps()
    23.     {
    24.         for (int i = 0; i < speedSprites.Count; i++)
    25.         {      
    26.             if (GameObject.Find ("Player").GetComponent<Player>().Spon)
    27.             {
    28.             SpeedCooldown = true;
    29.             speedSprites [i].fillAmount = player.maxSpeed;
    30.             }
    31.             if (SpeedCooldown == true)
    32.             {
    33.             speedSprites [i].fillAmount -= 1.0f/WaitTime * Time.deltaTime;
    34.                 print ("Unfilling");
    35.                 //speedSprites [i].fillAmount = player.curSpeed / player.maxSpeed;
    36.             }
    37.             else
    38.             {
    39.             speedSprites [i].fillAmount = 0f;
    40.                 SpeedCooldown = false;
    41.             }
    42.         }
    43.     }
    44. }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    once line 28 has been executed there is no way for SpeedCoolDown to ever be set to false again. Line 40 is never reached once SpeedCoolDown is true.

    Line 26 is wasteful, you already have a reference to the player, use that rather than finding it in the scene over and over


    don't see anything that would delay the unfilling though :confused: how is "spon" set? how does UpdatePowerUps() get called?
     
  3. alenmusic

    alenmusic

    Joined:
    Apr 7, 2015
    Posts:
    5
    Hi LeftyRighty,

    thanks for insight regarding the calling Player. Spon and UpdatePowerUps is called via player script:
    Code (CSharp):
    1. if (curSpeed > 5)
    2.         {
    3.             Spon = true;
    4.             SpeedDuration -= Time.deltaTime;
    5.             if (SpeedDuration < 0)
    6.             {
    7.                 curSpeed = 5;
    8.                 SpeedDuration = 5;
    9.                 Spon = false;
    10.                 GetComponent<AudioSource> ().PlayOneShot (SpeedOff, 0.7f);
    11.             }
    12.         }
    13.         PowerUpsController.UpdatePowerUps ();
    Next:
    Code (CSharp):
    1. public void TakeSpeed()
    2.     {
    3.         curSpeed += SpeedPickUp;
    4.         if (curSpeed >= 5)
    5.         {
    6.             curSpeed = maxSpeed;
    7.  
    8.         }
    9.         PowerUpsController.UpdatePowerUps ();
    10.     }
    And on trigger:
    Code (CSharp):
    1. if (other.tag == Tags.SPEED)
    2.             {
    3.             TakeSpeed ();
    4.             }
    PowerUpsController is game object in editor havin UpdatePowerUps script
     
    Last edited: Jul 1, 2015
  4. alenmusic

    alenmusic

    Joined:
    Apr 7, 2015
    Posts:
    5
    solved:
    Code (CSharp):
    1. if (player.Spon == true)
    2.             {
    3.             SpeedCooldown = true;          
    4.             speedSprites [i].fillAmount -= 0.35f/WaitTime * Time.deltaTime;
    5.             }
    6.             else if (player.Spon == false)
    7.             {
    8.             SpeedCooldown = false;
    9.             speedSprites [i].fillAmount = player.curSpeed / player.maxSpeed;
    10.             }