Math

From ETC Public Wiki
Jump to: navigation, search

Min Function

 Mathf.Min(n1,n2); 

Max Function

 Mathf.Max(n1,n2);

Mod Function

 num = (num + 1) % maxNum;


Using min and max to keep a value within a range

 var result : int = Mathf.Max(minValue, Mathf.Min( value, maxValue));


Div Function

 var div : int = float / n; 

since div is of type int no type cast is needed.


int division for health bar

 barLength = (1.0 * health / maxHealth) * barMaxLength;


Random numbers

Random.Range

static function Range (min : float, max : float) : float

static function Range (min : int, max : int) : int

Both min and max are included in the range of possible numbers


Random.value

static var value : float


 // Prints something between 0 and 1
 print(Random.value);
 // Loads a random level from the level list
 Application.LoadLevel(Random.Range(0, Application.levelCount));


Distance between two objects

 var other : Transform; 
 
 var dist = Vector3.Distance(other.position, transform.position);