Unity – Scripting – Math

Videogames Development – Unity – Mathf

The Math object allows you to perform mathematical tasks.

BASIC USE

Assign this code to an Empty Object in the Scene:

#pragma strict

function Start()
{
    
}

function Update()
{
var x=9;
var y=Mathf.Sqrt(x);
Debug.Log(y); // The result is 3
}

COMPLETE LIST

Deg2Rad Degrees-to-radians conversion constant (Read Only).
Epsilon A tiny floating point value (Read Only).
Infinity A representation of positive infinity (Read Only).
NegativeInfinity A representation of negative infinity (Read Only).
PI The infamous 3.14159265358979… value (Read Only).
Rad2Deg Radians-to-degrees conversion constant (Read Only).

Abs Returns the absolute value of f.
Acos Returns the arc-cosine of f – the angle in radians whose cosine is f.
Approximately Compares two floating point values if they are similar.
Asin Returns the arc-sine of f – the angle in radians whose sine is f.
Atan Returns the arc-tangent of f – the angle in radians whose tangent is f.
Atan2 Returns the angle in radians whose Tan is y/x.
Ceil Returns the smallest integer greater to or equal to f.
CeilToInt Returns the smallest integer greater to or equal to f.
Clamp Clamps a value between a minimum float and maximum float value.
Clamp01 Clamps value between 0 and 1 and returns value.
ClosestPowerOfTwo Returns the closest power of two value.
Cos Returns the cosine of angle f in radians.
DeltaAngle Calculates the shortest difference between two given angles.
Exp Returns e raised to the specified power.
Floor Returns the largest integer smaller to or equal to f.
FloorToInt Returns the largest integer smaller to or equal to f.
GammaToLinearSpace Converts the given value from gamma to linear color space.
InverseLerp Calculates the Lerp parameter between of two values.
IsPowerOfTwo Returns true if the value is power of two.
Lerp Interpolates between a and b by t. t is clamped between 0 and 1.
LerpAngle Same as Lerp but makes sure the values interpolate correctly when they wrap around 360 degrees.
LinearToGammaSpace Converts the given value from linear to gamma color space.
Log Returns the logarithm of a specified number in a specified base.
Log10 Returns the base 10 logarithm of a specified number.

Max Returns largest of two or more values.
Mathf.Max(1, 2)

Min Returns the smallest of two or more values.
Mathf.Min(1, 2)

MoveTowards Moves a value current towards target.
MoveTowardsAngle Same as MoveTowards but makes sure the values interpolate correctly when they wrap around 360 degrees.
NextPowerOfTwo Returns the next power of two value.
PerlinNoise Generate 2D Perlin noise.
PingPong PingPongs the value t, so that it is never larger than length and never smaller than 0.
Pow Returns f raised to power p.
Repeat Loops the value t, so that it is never larger than length and never smaller than 0.

Round Returns f rounded to the nearest integer.
If the number ends in .5, the even (pari) number is returned
Mathf.Round(10.2) // the result is 10
Mathf.Round(-10.7) // the result is -11

RoundToInt Returns f rounded to the nearest integer.

Sign Return value is 1 when f is positive or zero, -1 when f is negative.
Mathf.Sign(-10)

Sin Returns the sine of angle f in radians.
Mathf.Sin(3)

SmoothDamp Gradually changes a value towards a desired goal over time.
SmoothDampAngle Gradually changes an angle given in degrees towards a desired goal angle over time.
SmoothStep Interpolates between min and max with smoothing at the limits.

Sqrt Returns square root of f.
Mathf.Sqrt(9)

Tan Returns the tangent of angle f in radians.
Mathf.Tan(0.5)

OFFICIAL REFERENCE

http://docs.unity3d.com/Documentation/ScriptReference/Mathf.html

By |Unity3D|Commenti disabilitati su Unity – Scripting – Math

Unity – Scripting – OnClick – Counter

Videogames Development – Unity – OnClick Counter

A great snippet if you make a game that needs a click counter!

Code:

#pragma strict

function Start () {

}

// On Click Counter START #########################

//This is the variable we are using to store the number of clicks
var clickCounter: int;
//This creates a button and adds +1 to clickCounter variable every 1 click
function OnGUI () {
	if (GUI.Button (Rect (10,10,150,100), "You clicked:" + clickCounter)) {
	    clickCounter ++;		
	}
}

// On Click Counter END ###########################

function Update () {

}
By |Unity3D|Commenti disabilitati su Unity – Scripting – OnClick – Counter

Unity – Scripting – Random Numbers

Videogames Development – Unity – Random Numbers

Assign this code to an Empty Object in the Scene:

#pragma strict

var x : float;


function Start()
{
    x = Random.Range(-10.0, 10.0);
}

function Update()
{
    Debug.Log(x);
}

Statement:
x = Random.Range(-10.0, 10.0); -> static function Range(min: float, max: float): float; -> The result is a float numer as: 1.986345

By |Unity3D|Commenti disabilitati su Unity – Scripting – Random Numbers

Unity – Get Mouse Position

Videogames Development – Unity – Get Mouse Position

SCRIPT

MAIN TOP MENU> ASSETS> CREATE> Javascript, type the name ‘GetMousePosition’

Assets> GetMousePosition> Inspector> ‘Open…’ button

Write:

#pragma strict

function Start () {

}

function Update () {
	var mouse = Input.mousePosition;
        Debug.Log(mouse);
		
	}

Create an Object inside the Hierarchy ‘MyObject’

Assets> DRAG AND DROP ‘GetMousePosition’ script over Hierarchy> MyObject

Play> move the mouse, look at the BOTTOM OF THE SCREEN, you will see the coordinates as: (536.0, 583.0, 0.0)

The current mouse position in pixel coordinates. (Read Only)

The bottom-left of the screen or window is at (0, 0).
The top-right of the screen or window is at (Screen.width, Screen.height).

By |Unity3D|Commenti disabilitati su Unity – Get Mouse Position

Unity – WASD – Basics

Videogames Development – Unity – How to add WASD

PREFERENCES

Horizontal

MAIN TOP MENU> Edit> Project Settings> Input> Inspector> Horizontal:
Negative Button: left -> Alt Negative Button: a
Positive Button: right -> Alt Positive Button: d

Vertical

MAIN TOP MENU> Edit> Project Settings> Input> Inspector> Vertical:
Negative Button: down -> Alt Negative Button: s
Positive Button: up -> Alt Positive Button: w

NOTICE: they are classic – W A S D – controller for left hand

SCRIPT

MAIN TOP MENu> ASSETS> CREATE> Javascript, type the name ‘GetInput’

Assets> GetInput> Inspector> ‘Open…’ button

Write:

#pragma strict

function Start () {

}

function Update () {

 var horiz : float = Input.GetAxis("Horizontal");
 Debug.Log(horiz);

}

Create an Object inside the Hierarchy ‘MyObject’

Assets> DRAG AND DROP ‘GetInput’ script over Hierarchy> MyObject

Play> Use – A D – buttons, look the BOTTOM OF THE SCREEN, click label ‘Console’ to see the input values.

Write:

#pragma strict

function Start () {

}

function Update () {

 var horiz : float = Input.GetAxis("Horizontal");
 Debug.Log(horiz);
 transform.Translate(Vector3(horiz,0,0));
}

Play> Use – A D – buttons to move object

NOTICE:

transform.Translate(Vector3(horiz,0,0));

It gets X position from horizon variable and Translate (move) the Object

By |Unity3D|Commenti disabilitati su Unity – WASD – Basics