Formula

Updated: 2018-08-29

Formula is a powerful unit that allows you to evaluate logical and mathematical expressions directly via a textual Formula and a list of Arguments.

Performance: At the moment, using the formula unit is significantly slower than using the operator units individually, because of the binary tree traversal overhead (despite caching attempts). It is preferable to avoid using this unit at every frame.

Most often, you'll want a formula to return either a boolean for logic, or a number for math, but formulas can return any type of value.

A new formula looks like this:

The first text field in the header is the formula itself.

The second text field is the amount of arguments. It's set to 2 by default, giving us A and B as inputs. Formulas can have up to 10 arguments, which are always ordered alphabetically. If you add more, they will be called B, C, D, E, and so forth.

For example, this formula returns a boolean indicating whether at least 10 seconds have elapsed since the start of the game and the current object's name is Player: (because... reasons).

Arguments

Variable Names

You can also directly use variable names in the formula. For example, if you have a graph variable named health, you could return a boolean just by typing the formula health > 50. The argument names are evaluated in the following order of priority:

  1. Alphabetical argument names (a - z)
  2. Graph variable names
  3. Object variable names
  4. Scene variable names
  5. Application variable names
  6. Saved variable names

Properties and Methods

You can retrieve the value of a property on an argument or variable by using the [arg.prop] notation.

For example, if position is a Vector 3 object variable, you can check if it is equal to zero with: [position.x] = 0 .

You can also get the return value of parameterless methods with the [arg.Method()] notation.

Note that accessing properties and methods is not guaranteed to be compatible with AOT platforms, because the AOT pre-build cannot generate stubs for members that are only accessed by name.

Literals

You can use the following literals:

LiteralDescriptionExample
NumberAn integer or float.3.5
StringA piece of text between apostrophes."Hello World!"
BooleanA boolean value.true, false
NullThe null constant.a != null
Delta TimeThe Unity frame delta time.30 * dt
Invert Delta TimeThe inverse of the delta time.30 / second


Operators

You can use every common logical and mathematical operator in formulas, even the ones defined through custom operator overloading in script. Here's an overview:

OperatorOperationRankResultExample
not, !Logical NegationUnaryThe opposite of the operand.not true
-Numerical NegationUnaryThe negative of the operand.-5
and, &&Logical AndBinaryTrue if both operands are true.(a < 5) and (b > 3)
or, ||Logical OrBinaryTrue if either operand is true(a < 5) or (b > 3)
=, ==EqualityBinaryTrue if the two operands are equal.a = b
!=, <>InequalityBinaryTrue if the two operands are not equal.a != b
<, <=, >, >=Numeric ComparisonBinaryThe result of a numeric comparisona >= 10
+AdditionBinaryThe sum of the two operands.a + 5
-SubtractionBinaryThe difference between the two operands.b - 3
*MultiplicationBinaryThe product of the two operands.12 * a
/DivisionBinaryThe quotient of the two operands.b / 2
%ModuloBinaryThe remainder of the division of the two operands.a % 2
?:IfTernaryThe left operand if the condition is true, otherwise the right operand.(health > 0) ? "Alive" : "Dead"

All common bitwise operators like ~ and >> are also supported.

Functions

You can also use any function from this table:

NameResultExample
absThe absolute value of a specified number.abs(-1)
acosThe angle whose cosine is the specified number.acos(1)
asinThe angle whose sine is the specified number.asin(0)
atanThe angle whose tangent is the specified number.atan(0)
ceilingThe smallest integer greater than or equal to the specified number.ceiling(1.5)
cosThe cosine of the specified angle.cos(0)
expe raised to the specified power.exp(0)
floorThe largest integer less than or equal to the specified number.floor(1.5)
logThe logarithm of a specified number.log(1, 10)
log10The base 10 logarithm of a specified number.log10(1)
maxThe larger of two specified numbers.max(1, 2)
minThe smaller of two numbers.min(1, 2)
powA specified number raised to the specified power.pow(3, 2)
roundRounds a value to the nearest integer or specified number of decimal places.round(3.222, 2)
sign1 if the number is positive, -1 is if it negative.sign(-10)
sinThe sine of the specified angle.sin(0)
sqrtThe square root of a specified number.sqrt(4)
tanThe tangent of the specified angle.tan(0)
truncateThe integral part of a number.truncate(1.7)
v2Creates a 2D vector.v2(0, 0)
v3Creates a 3D vector.v3(0, 0, 0)
v4Creates a 4D vector.v4(0, 0, 0, 0)
Was this article helpful?
Be the first to vote!
Yes, helpful
No, not for me