Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

What is a Vector2 and Vector3 in Unity?

Writer Sophia Terry

Title says it all. I see them used in movement scripts often, if that helps. What is a Vector2 and a Vector3, the Unity docs are a bit hard to follow for new people.

1

3 Answers

Vector's are mathematical models that model both direction and magnitude. A Vector2 is 2D, and a Vector3 3D.

A vector2(1,5) is a direction with the ratio of 1 part x, and 5 parts y. E.G a line 1/6th to the right, and 5/6th's up. a negative would make the line left and down respectively.

Magnitude show's the "strength" of the direction. E.g when using forces and physics, pushing something in a vector2(1,0) is a much weaker push to the right than a vector2(100,0) even though the direction is identical.

That should be a basic theory introduction for you.

0

A Vector is basically a quantity which has a direction: The quantity is referred to as the magnitude of the vector, the direction is referred to as the normalized vector.

Vector is represented using its components: the projection of the vector on each axis is referred to as the components of the vector.

enter image description here

A Vector1 has a 1D direction, like a point on a line, or the value of a steering wheel, or any real number. e.g. (0) or (-1000). The magnitude of a Vector1 equals the absolute value of the x component of the vector or sqrt(x^2).

enter image description here

A Vector2 has a 2D direction, like a xy point in a 2D space, or the position of a joystick stick, or the uv offset of a point on a 2D texture. e.g. (0,0) or (-1, 100). The magnitude of a Vector2 equals sqrt(x^2+y^2).

enter image description here

A Vector3 has a 3D direction, like a xyz point in a 3D space, or a color in RGB format, or a set of three numbers. e.g. (0,0,0) or (-0.1, 3.14, 30). The magnitude of a Vector3 equals sqrt(x^2+y^2+z^2).

enter image description here

A Vector4 has a 4D direction, like a xyzw point in a 4D space, or a color in RGBA format, or a set of four numbers. e.g. (0,0,0,0) or (0.1, 0.2, 0.3, 0.4). The magnitude of a Vector4 equals sqrt(x^2+y^2+z^2+w^2).

enter image description here

4

Vector2

It is representation of 2D vectors and points, used to represent 2D positions, only two axis x&y.

Code Examples

for Positive values

Debug.log(new Vector2(1, 2) - new Vector2(3, 2));

for Negative values

print(-new Vector2(1, 2));

Vector3

It is representation of 3D vectors and points, used to represent 3D positions,considering x,y & z axis.

Code Examples

Vector3 offset = transform.position - player.transform.position;

1.transform.Translate(Vector3.forward * Time.deltaTime * speed)

  1. Debug.Log(Cube.transform.rotation) //cube is the Gameobject

The Vector 3D response

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy