Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Jan 06, 2017 at 01:30 AM by JoshBaz for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by JoshBaz · Dec 28, 2015 at 04:02 PM · camera-movementvariablestimers

HELP! How to use a timer to set variables?

Hello again. This problem has been bugging me in every scene I try to make. So basically I have a timer variable(a float) and any other variable(an int or bool). So what i want to do is based on the value my timer variable has, the value of another variable will change. Example "if(timer >= 10.0f){ goal = true; }" or something like that. Here's some of my code and a screenshot of what I mean to further explain(also ignore the missing script error, that's for something else):

 public int posNum;
 public float cameraTimer;
 public bool isIdle = true;
 public bool isViewing = false;
 
 // Update is called once per frame
     void Update () {
         cameraTimer += Time.deltaTime;
 
         if(isIdle){
             //Sets camera position number
             if(cameraTimer == 0.1f){
                 posNum = 0;
             }else if(cameraTimer == 8.2f){
                 posNum = 1;
             }else if(cameraTimer == 16.1f){
                 posNum = 2;
             }
             IdleCam();
         }
         if(isViewing){
 
         }
 
 
     }

alt text

As you can see the cameraTimer has passed 8.2f and the camPos variable has not been set to 1. How do I fix this? Thanks.

screenshot-15.png (403.8 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

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by jmonasterio · Dec 28, 2015 at 04:06 PM

The timer will never be exactly equal to 0.1f, 8.2f, and 16.2f. Comparing floats with == is a bad idea, since the floating point value may be off by a tiny amount like 0.1001. Especially with timers, the time doesn't go up by a fixed amount, so it will almost never ever be equal.

Use: >=

Like:

 if(cameraTimer >= 0.1f){
                  posNum = 0;
              }else if(cameraTimer >= 8.2f){
                  posNum = 1;
              }else if(cameraTimer >= 16.1f){
                  posNum = 2;
              }
Comment
Add comment · Show 2 · 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 JoshBaz · Dec 28, 2015 at 05:06 PM 0
Share

Thanks for the answer. But heres a followup question: Could I do something like: if(cameraTimer > 0.1f &&& cameraTimer <8.1f){ posNum = 0; }

I don't know if that's correct. What I'm trying to say could I do an IF statement where the float variable has to be between a certain range of values. Thanks again.

avatar image jmonasterio JoshBaz · Dec 28, 2015 at 11:30 PM 0
Share

You could put the code in different order and it would do what you want without so many &&'s.

       if(cameraTimer >= 16.1f){
           posNum = 2;
       }else if(cameraTimer >= 8.2f){
           posNum = 1;
      }
     else if(cameraTimer >= 0.1f){
           posNum = 0;

avatar image
0

Answer by Priyanshu · Dec 28, 2015 at 04:06 PM

To check your mistake, simple print cameraTimer value in Update. By doing this you can see that cameraTimer will never be equal to .1/8.2/16.1 but equivalent to values like .12345/ 8.2323/16.12343.

Therefore its more feasible to use greater Than or Less Than operators. for such purpose.

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I assign a value into my object.,How to assign a variable to a sprite that is used as an object and is randomly spawned? 0 Answers

Changing variable if there are many scripts. 0 Answers

How to deduct money from another script when buying in Unity? 0 Answers

How to turn camera 90 degrees smoothly? 1 Answer

Camera movement with arrow keys? 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