- Home /
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!
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);
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?
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.
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.)
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.
Answer by IR_HellBlaDe · Jul 23, 2014 at 05:32 AM
check this out : http://forum.unity3d.com/threads/converting-float-to-integer.27511/
Your answer
Follow this Question
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