Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by moonLite · Oct 21, 2012 at 12:46 PM · animationtimetime.deltatimetimer countdownanimated-texture

How can the animation of time bar become smoother?

Hi Guys,

The issue I have with my time bar is that when it's set to 120sec. The time bar is moving like stop motion animation. But if I set to 20 or 10 sec, it the animation of the time bar is smoother. So my question is

1) Why? above

2) How can i make the animation of my time bar become smoother? just like when it's set to 10 or 20sec

3) How can i make use of time.deltatime to do that? I only know time.deltatime is used like to move how many units per second.

Below is my code:

 #pragma strict

 var clockIsPaused : boolean = false;
 var startTime : float; // (in sec)
 var timeRemaining : float; // (in sec)
 
 var percent : float;
 var clockBG : Texture2D;
 var clockFG : Texture2D;
 var clockFGMaxWidth : float; // the starting width of the foreground bar
 
 
 function Awake()
 {
     startTime = 120.0;
     clockFGMaxWidth = clockFG.width;
 }
 
 function Update () 
 {
     if(!clockIsPaused)
     {
         // make sure the timer is not paused
         DOCountdown();
     }
 }
 
 function OnGUI()
 {
     var newBarWidth : float = (percent/100) * clockFGMaxWidth; // this is the width that the foreground bar should be
     var gap : int = 50; // a spacing var to help us position the clock    
     
     GUI.BeginGroup (new Rect(Screen.width - clockBG.width - gap, gap, clockBG.width, clockBG.height));
     GUI.DrawTexture (Rect (0, 0, clockBG.width, clockBG.height), clockBG);
         GUI.BeginGroup (new Rect(5, 6, newBarWidth, clockFG.height));
         GUI.DrawTexture (Rect (0, 0, clockFG.width, clockFG.height), clockFG);
         GUI.EndGroup ();
     GUI.EndGroup();
 }
 
 function DOCountdown()
 {
     timeRemaining = startTime - Time.time;
     
     percent = timeRemaining/startTime * 100;
     if (timeRemaining < 0)
     {
         timeRemaining = 0;
         clockIsPaused = true;
         TimeIsUp();
         Debug.Log("Time is up!");
     }
     ShowTime();
 }

Thanks in advance!

Below is the image assets I'm using, it's from the book Unity 3.x Game Development by Example Beginner's Guide by Ryan Henson Creighton.

alt text ClockBG image
alt text ClockFG image

clockbg.jpg (3.2 kB)
clockfg.jpg (2.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Griffo · Oct 21, 2012 at 02:07 PM

How big is your clockBG and clockFG Textures? I'm sure the 120 seconds wont run down smooth because they are not wide enough (not enough pixels, 120) in the edited scrip below I've extended the GUI's width and if you input 120 it runs down smooth because the Texture I use is 64 x 64 and I * it by 2 to get 128 so now it runs down 1 pixel + a bit every second, and looks smooth.

So if you only use a 64 x 64 it will look like it pauses because it will run down a pixel every 2 seconds, correct me if I'm wrong.

 #pragma strict
 
 var clockIsPaused : boolean = false;
 var startTime : float; // (in sec)
 var timeRemaining : float; // (in sec)
 
 var percent : float;
 var clockBG : Texture2D;
 var clockFG : Texture2D;
 var clockFGMaxWidth : float; // the starting width of the foreground bar
 
 
 function Awake()
 {
     //startTime = 20.0;
     clockFGMaxWidth = clockFG.width * 2;
 }
 
 function Update () 
 {
     if(!clockIsPaused)
     {
        // make sure the timer is not paused
        DOCountdown();
     }
 }
 
 function OnGUI()
 {
     var newBarWidth : float = (percent/100) * clockFGMaxWidth; // this is the width that the foreground bar should be
     var gap : int = 100; // a spacing var to help us position the clock 
 
     GUI.BeginGroup (new Rect(Screen.width - clockBG.width - gap, gap, clockBG.width * 2, clockBG.height));
     GUI.DrawTexture (Rect (0, 0, clockBG.width * 2, clockBG.height), clockBG);
        GUI.BeginGroup (new Rect(0, 0, newBarWidth, clockFG.height));
        GUI.DrawTexture (Rect (0, 0, clockFG.width * 2, clockFG.height), clockFG);
        GUI.EndGroup ();
     GUI.EndGroup();
 }
 
 function DOCountdown()
 {
     timeRemaining = startTime - Time.time;
 
     percent = timeRemaining/startTime * 100;
     if (timeRemaining < 0)
     {
        timeRemaining = 0;
        clockIsPaused = true;
        //TimeIsUp();
        Debug.Log("Time is up!");
     }
     //ShowTime();
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image moonLite · Oct 26, 2012 at 01:07 PM 0
Share

@Griffo,

you changed the GAP only right? that is for positioning the time bar only.

the ClockFG (more infont layer) is 243 x 39 pixel the ClockBG (background layer) is 254 x 51 pixel

THe one is that being move is ClockFG. If I put 120sec, why couldn't it animate smoothly since it's 243 pixel width? It should be enough.

I will upload the picture via here, for others to see.

If I put it with 120sec, it will be like old clock the sec hand moving but in this case it's time bar.

if I put 5 sec, it's very smooth animation.

If I put 20 sec , it's not as smooth as as 5sec.

If I put 40sec, it's getting worse. $$anonymous$$ore like 120sec.

So in other words, the longer time length I input, the more lousy the animation of the time bar is .

So are you saying that if i use a bigger picture like more pixel, it will be smoother in the animation even when my time limit is set to like 120sec?

avatar image moonLite · Oct 26, 2012 at 02:06 PM 0
Share

Just to update, I actually replace it with SUPER big picture like more than 2000 x 2000 pixel. And I scale to fill it. it's still has the same a little stop motion effect.

I did have round clock timer, their animation works very smooth. Though they're using matrix & vector to turn.

avatar image KnightRiderGuy · Dec 14, 2014 at 03:47 PM 0
Share

Thanks for posting this, this is exactly the type of thing I was looking for to make time slowly count down to extinction for an Oxygen tank. $$anonymous$$y only question is can this be made to layer behind a texture GUI overlay with a window cut out to show the progress bar?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

delta time is not working right (freezed) 0 Answers

Time.deltaTime not working correctly? 1 Answer

timer that can be reset 1 Answer

Frame rate independent animation not working with Time.deltaTime or Coroutines 1 Answer

Can I make animations snap to a frame? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges