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 Joahun · Oct 30, 2014 at 05:53 PM · javascripttimer

Set timer to a specific time.

Hello, im a begginer javascript programer and i mad a game clock. it shows 24 real life minutes whic is a day in my future game. You can pause it set the morning time and the dawn time. Now i got two booleans, setToDayTime and setToNigthTime and i want these to set the clock to the morning time and the dawn time and continue the counting from there but i havent managed to make it in the past two days. Can someone help me out? #pragma strict

 var mutato1 : GameObject;
 var mutato2 : GameObject;
 var mutato3 : GameObject;
 var mutato4 : GameObject;
 
 var dayTimeStartHour : int = 6;            //morning start
 var dayTimeEndHour : int = 18;            //morning end
 
 var setToDayTime : boolean = false;        //set clock to morning start
 var setToNigthTime : boolean = false;    //set clock to morning end
 var isItDayTime : boolean = false;
 
 var pauseGameTime : boolean = false;    //pause game time
 var displayTime : float;                //actual time displaying
 var difference: float;                     // to pause game time
 private var ido2 : float;
 
 
 function aniSprite ( spriteObject : GameObject, columnSize : float, rowSize : float, type:String, index : int) // animating spirtes 0-9
 {
     var minute1 = index % 10;
     var minute2 = (index/10) % 6;
     var minute3 = (index / 60) % 10;
     var minute4 = (index / 600) % 2.4;
     
 
     if(type == "font1")    index = minute1;
     if(type == "font2")    index = minute2;
     if(type == "font3")    index = minute3;
     if(type == "font4")    index = minute4;
 
     var size : Vector2 = Vector2 ( 1.0 / columnSize, 1.0 / rowSize );    // find scale to show on poly 
     var offset : Vector2 = Vector2(index * size.x ,1);
     
     spriteObject.renderer.material.mainTextureOffset = offset;     // apply the offset amount to the correct sprite sheet object
     spriteObject.renderer.material.mainTextureScale  = size;     // apply the scale amount to the correct sprite sheet object
 
 }
 
 function Update ()
 {
         
     var ido : float = Time.time - ido2;
         
     if(!pauseGameTime)                        //pause gametime
         {
         displayTime = ido - difference;
         ido -= difference;
         
         }
 
     if(pauseGameTime)                        //continue gametime
         {
             difference= ido - displayTime;
         }
     
 
     if( ido >= 1440)       //reset timer from 1440 seconds (24min) to 0
         {
             ido2 = Time.time - difference;
         }
 
     if(displayTime >= dayTimeStartHour * 60 && displayTime <= dayTimeEndHour * 60)
         {
          isItDayTime = true;
         }
         else
         {
             isItDayTime = false;
         }
 
 
     if(setToDayTime)
         {
             displayTime = dayTimeStartHour * 60;
             setToDayTime = false;
         }
         if(setToNigthTime)
         {
             displayTime = dayTimeEndHour * 60;
             setToNigthTime = false;
         }
 
     aniSprite (mutato1, 10, 1, "font1", displayTime);
     aniSprite (mutato2, 10, 1, "font2", displayTime);
     aniSprite (mutato3, 10, 1, "font3", displayTime);
     aniSprite (mutato4, 10, 1, "font4", displayTime);
 }
 
 
 
     
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
1

Answer by Kiwasi · Oct 30, 2014 at 06:12 PM

Your structure is a bit of a nightmare. The class determining the time should have nothing to do with the various animations. You may find it easier if you remove all of the junk that is not related to keeping time to a different script.

Following your logic and variable names is a bit painful. The typical way to use a timer is to set a float to the time, then use a bunch of properties to convert that into the display time. There seems to be little point to keeping an offset to measure pause time, as this is automatically accounted for by using Time.time or Time.realTimeSinceStartUp. (Depending on which effect you are after).

Here is an alternative way to code a clock. (C#) Pseudo code, it doesn't do exactly the same as your class, but you get the idea.

 float time;
 public float hour {
     get {return time/60/60 % 24;}
     set {
         time = value * 60 * 60;
     }
 }
 // And so forth
 
 void Update (){
     time += Time.deltaTime;  // or Time.unscaledDeltaTime
 }
 
 void SetMorning (){
     hour = 6;
 }

Comment
Add comment · Show 1 · 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 Joahun · Oct 30, 2014 at 06:40 PM 0
Share

Thank you for your answer and for leading me to the rigth way. Im going to start the whole thing again with a clear $$anonymous$$d. Best wishes!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Adding force to a projectile? (SOLVED) 1 Answer

BCE0044: expecting }, found 'private'. PLEASE HELP!!!!! 2 Answers

Script Not Working At All.... :) (SOLVED) 1 Answer

Errors with script using Javascript. 2 Answers


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