x


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!

more ▼

asked May 29 '11 at 11:33 PM

superventure gravatar image

superventure
649 44 54 63

note, I am using Js

May 29 '11 at 11:35 PM superventure

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

Jun 11 '11 at 06:51 AM superventure
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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);
more ▼

answered May 29 '11 at 11:58 PM

aldonaletto gravatar image

aldonaletto
41.6k 16 42 197

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?

May 31 '11 at 07:03 AM superventure

If you want the checker variable to have no decimals, you MUST 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 Mathf.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.

May 31 '11 at 01:11 PM aldonaletto
(comments are locked)
10|3000 characters needed characters left

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.)

more ▼

answered May 29 '11 at 11:50 PM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered May 31 '11 at 08:24 AM

cjmarsh gravatar image

cjmarsh
373 2 9

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x212
x182
x114
x23

asked: May 29 '11 at 11:33 PM

Seen: 5097 times

Last Updated: Jun 11 '11 at 06:51 AM