Operating on numbers in UltEvents is tricky. We generally use either the X/Y/Z position of a Transform to store numbers that need to change.
Counters can be super useful for logic - you could create thrusters with customisable speed, cycle between skins on a spawnable, or feed the value directly into a shader to customisable the look of something.
Increasing the Counter
Transform.Translate
adds the Vector we provided it to our Transformâs position:
Transform.Translate is used to add 1 to our Transformâs X position each time.
Getting a Float from our Counter
To get the value of our counter, we can check the distance between 0, 0, 0
and its current local position.
Why use local position instead of position?
Unlike
Transform.position
,Transform.localPosition
isnât influenced by anything above it in the hierarchy. This is important as say, we wouldnât want the position of our vehicle to effect our counter thatâs parented underneath it.
The counterâs value (as stored in the Transformâs position) is converted to a float and outputted to the Delayed Ult Event Holderâs Delay for visual purposes.
Getting an Integer from a Counter
MathF.RoundToInt
rounds floats to whole numbers and outputs the integer.
Always round when working with whole numbers!
When working with whole numbers, try to always round the number first. This avoids problems with floating-point imprecision.
Clamping
We donât always want our counter to be able to increase and decrease infinitely.
Clamping the Maximum Value
Vector3.Min
takes two Vector3s, and choses the smallest XYZ components from each.
Whenever our counter goes up, we can use Vector3.Min
to overwrite it if it goes above a certain value:
The logic on the ClampMaxValue GameObject is ran whenever the counter goes up. A minimum value between X: 4, Y: 0, Z: 0
prevents our counter from going above X: 4, Y: 0, Z: 0
Clamping the Minimum Value
Similarly, Vector3.Max
takes two Vector3s, and choses the largest XYZ components from each.
We can use this to stop our counter from going below a certain value, or combine it with the previous logic to clamp up and down:
The new logic on the ClampMinValue GameObject selects the max XYZ components between the counterâs local position and X: 0, Y: 0, Z: 0
. This stops it from going below 0. Combined with the previous logic on âClampMaxValueâ, our counter is now limited between values 0 and 4!
Prefabs
These can be dropped into your Unity project and contain pre-made logic:
- Clamped Counter.prefab - A counter that increases and decreases, but is constrained within a certain value range.
- Simple Counter.prefab - A counter that increases and decreases.