Daniele Pappalardo
Daniele Pappalardo

Reputation: 104

Alter variable by an amount in real time seconds

I have a float value that I would like to decrease by 10.f per second; for example

floatx - 10f * 'second'

How can I get real time seconds or another form of time in unity?

Upvotes: 0

Views: 271

Answers (1)

Jay
Jay

Reputation: 2946

The easiest way to do this would be in Update:

var -= 10.f * Time.deltaTime;

or, if the variable you are modifying is going to be affecting the movement of a physics object, then in fixedUpdate:

var -= 10.f * Time.fixedDeltaTime;

or if you alter your deltaTime for any reason (such as to create a slow motion effect) and you want this to happen in real time seconds regardless of this

// in update
var -= 10.f * Time.unscaledDeltaTime;

or if it's altering something about a physics object

// in fixedUpdate
var -= 10.f * Time.fixedUnscaledDeltaTime

All these will reduce your variable by 10 per second

Upvotes: 2

Related Questions