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 /
avatar image
1
Question by superventure · May 29, 2011 at 11:33 PM · mathfloatintround

Float to Int

What am I doing wrong here? I just need to var checker to have no decimals....

 var checker = 0;
 var checkerSpeed : float = .03;
 
     function Update(){
     
     //a timer
     checker += Mathf.FloorToInt(1 * Time.deltaTime * checkerSpeed);

Thank you!

Comment
Add comment · Show 2
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 superventure · May 29, 2011 at 11:35 PM 0
Share

note, I am using Js

avatar image superventure · Jun 11, 2011 at 06:51 AM 0
Share

parseInt() was what I looking for, for anyone else.

4 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by aldonaletto · May 29, 2011 at 11:58 PM

Mathf.FloorToInt returns zero because deltaTime is a small number. Do it this way:

 var checker = 0;
 var checkerSpeed:float = .03;
 private var checkerAc:float = 0; // time accumulator
 
 function Update(){
 
   checkerAc += Time.deltaTime*checkerSpeed;
   checker = Mathf.FloorToInt(checkerAc);
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 superventure · May 31, 2011 at 07:03 AM 0
Share

Is there another way other than creating a new variable? I have many timers and if each already have 2 vars, well, adding a third for each just seems like too much. Is there any way to do this with one line of code?

avatar image aldonaletto · May 31, 2011 at 01:11 PM 0
Share

If you want the checker variable to have no decimals, you $$anonymous$$UST have a 3rd var (a float to accumulate the small increments). As an alternative, you can declare checker as a float, accumulate increments on it and read its integer part when needed with $$anonymous$$athf.Floor - but this approach will be more expensive if you need this value more than once in your function. If you're thinking about performance, declare your variables as private unless you really need them to appear in the Inspector. Declare also each variable's type - it usually doesn't impact performance, but avoid wrong type inference by the compiler, a very frequent headache cause.

avatar image
1

Answer by Eric5h5 · May 29, 2011 at 11:50 PM

You want checker to go up at intervals?

 var checker = 0;
 var interval = .3;
 
 function Start () {
     InvokeRepeating("IncreaseChecker", 0, interval);
 }
 
 function IncreaseChecker () {
     checker++;
 }

(As an aside, there's never any point to multiplying something by 1. Something * 1 = something.)

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
avatar image
-1

Answer by cjmarsh · May 31, 2011 at 08:24 AM

You can't actually compare real numbers to integers, so just stick with one or the other. If you using the reals, you'll have to use Mathf.Floor or something similar.

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
avatar image
0

Answer by IR_HellBlaDe · Jul 23, 2014 at 05:32 AM

check this out : http://forum.unity3d.com/threads/converting-float-to-integer.27511/

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Is it possible to round down 0.99 to 0? 2 Answers

How to round or convert doubles? 2 Answers

"bleeding" when using configurable joints 2 Answers

Periodic fraction gets converted to int 1 Answer

Why does the decimal point matter in my for loop? 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