Video Games Development

Unity – 2D Vector Math

Unity – 2D Vector Math

2D Vector Math uses Pythagoras’s Theorem.
It has 2 components, x and y, that rapresent the point from origin 0,0 to any point on a 2D plane.

A vector has:
– Magnitude, it is the lenght of the vector
– Direction, it is the direction of movement

Below an example to calculate the distance between 2 characters

unity-vector-math-img-001

unity-vector-math-img-002

Vector Math can be used to predict position of moving objects.
Below the character has a velocity o 12,5 per hour, it means it moves 12 units in x and 5 units in y per hour.
You can calculate the final position after one hour with vector math:

unity-vector-math-img-003

Unity has vector math functions.

Official guide at: http://docs.unity3d.com/Documentation/ScriptReference/Vector2.html

Calculate distance between two objects

var distance = Vector2.Distance(object1.transform.position, object2.transform.position);

Statement: Vector2.Distance(a,b) is the same as (a-b).magnitude.

By |Unity3D|Commenti disabilitati su Unity – 2D Vector Math

Unity – Hierarchies

Unity – Hierarchies

One of the most useful tools in producing computer animation is the ability to link objects together to form a chain. By linking one object to another, you create a parent-child relationship. Transforms applied to the parent are also transmitted to child objects. A chain is also referred to as a hierarchy.

1. MAIN TOP MENu> GameObject> Create Other> Cube

2. MAIN TOP MENu> GameObject> Create Other> Phere

3. MAIN TOp MENU> Window> Hierarchy> Drag The Sphere over the Cube> Sphere (child) Cube (parent)

4. In the viewport move the Sphere and move the Cube

5. Change on TOP LEFT Pivot/Center rotation and try rotate parent.

Select by name: Hierarchy Window> Search box: ‘t:light’ to show all lights

By |Unity3D|Commenti disabilitati su Unity – Hierarchies

Unity – Translate GameObjects

Unity – Translate GameObjects

Translate Meters x frame

DRAG AND DROP this script over a object in the Hierarchy:

#pragma strict

function Update ()
{
        transform.Translate(new Vector2(1,0));
}

Statement: transform.Translate(new Vector2(1,0)); -> transform.Translate(new Vector2(x,y));

It adds 1px of ‘x’ value of the 2D Vector. The object moves quickly because the transformation command is inside Update(), it means the code adds 1px every frame.

Translate Meters x seconds

DRAG AND DROP this script over a object in the Hierarchy:

#pragma strict

function Update ()
{
        transform.Translate(new Vector2(1,0)*Time.deltaTime);
}

NOTICE: *Time.deltaTime -> It converts the movement from meters x frame to meters x second

Translate Meters x frame + add velocity

DRAG AND DROP this script over a object in the Hierarchy:

#pragma strict

public var moveSpeed : float = 10f;

function Update ()
{
        transform.Translate(new Vector2(1,0)* moveSpeed *Time.deltaTime);
}

Invert movement

#pragma strict

public var moveSpeed : float = 10f;
public var xpos : float = 1;
public var ypos : float = 1;

function Update ()
{
        transform.Translate(new Vector2(xpos,ypos)* moveSpeed *Time.deltaTime);
}

NOTICE:
xpos= 1 move right
xpos= -1 move left

ypos= 1 move up
ypos= -1 move down

Vector2.right – Vector2.up

Vector2.right -> it this the same of -> new Vector2(1,0)
Vector2.up -> it this the same of -> new Vector2(0,1)

You can write:

#pragma strict

public var moveSpeed : float = 10f;
public var xpos : float = 1;
public var ypos : float = 1;

function Update ()
{
        transform.Translate(xpos*Vector2.right * moveSpeed * Time.deltaTime);
        transform.Translate(ypos*Vector2.up * moveSpeed * Time.deltaTime);
}

NOTICE:
xpos= 1 move right
xpos= -1 move left

ypos= 1 move up
ypos= -1 move down

By |Unity3D|Commenti disabilitati su Unity – Translate GameObjects

Unity – Rotate GameObjects

Unity – Rotate GameObjects

Rotate x frame

DRAG AND DROP this script over a object in the Hierarchy:

#pragma strict

function Update ()
{
         transform.Rotate(Vector3.back);
}

Rotate x frame

Rotate x seconds

#pragma strict

public var turnSpeed : float = 5f;

function Update ()
{
         transform.Rotate(Vector3.back, turnSpeed * Time.deltaTime);
}

Time.deltaTime -> it converts to rotate x seconds
turnSpeed -> speed of the rotation (it is a public variable, you can try to change his value inside Inspector!)

Rotate X Y Z

#pragma strict

public var turnSpeed : float = 20f;

function Update ()
{
         // -----------------------------
         // 2D Rotation XY plane
         // Rotate clockwise Z
         transform.Rotate(Vector3.back, turnSpeed * Time.deltaTime);         
         // Rotate anticlockwise Z
         transform.Rotate(-1*Vector3.back, turnSpeed * Time.deltaTime);
         
         // -----------------------------
         // 3D Rotation XZ and YZ
         // Rotate Y
         transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
         transform.Rotate(-1*Vector3.up, turnSpeed * Time.deltaTime);
         // Rotate X
         transform.Rotate(Vector3.right, turnSpeed * Time.deltaTime);
         transform.Rotate(-1*Vector3.right, turnSpeed * Time.deltaTime);
}
By |Unity3D|Commenti disabilitati su Unity – Rotate GameObjects

Unity – Game Objects – 2D Position

Unity – Game Objects – 2D Position

2D Position

The position of the object in world space.
NOTICE: x,y,z -> 0,0,0 it is the center of the screen

#pragma strict

function Update ()
{     
        // Move the object to (0, 0, 0)
	transform.position = Vector3(0, 0, 0);
		
	// Print the x component of the position to the Console
	print(transform.position.x);
	print(transform.position.y);
	print(transform.position.z);
}

Statement: transform.position = Vector3(x, y, z);

NOTICE: in 2D Games z=0

By |Unity3D|Commenti disabilitati su Unity – Game Objects – 2D Position