# New to Unity, got lots of questions, Vector2 and Vector3?

Hi all, I’m new to Unity. It is quite confusing.
So about the Vector2 and Vector3. Are the “vector” here different to the vector in mathematics?
The vector in mathematics has two components? - magnitude and direction
So a Vector2 is defined by the point’s x axis value and y axis value whereas Vector3 has an additional z axis value.
Is the direction information included in Vector2 and Vector3?

I couldn’t find direction variable nor any function regarding direction in the Vector2/3 document.

Depending on how you view your vector variable, it could represent a displacement vector, a position vector or just 3 floats.

The x and y value for Vector 2 (x, y and z value for Vector3) would represent the direction or position, and the .sqrMagnitude would be the magnitude of the Vector.

Could you please demonstrate how to get the direction? for both Vector2 and Vector3 type. Thanks. Do they merely store the coordinates of a location? The vector in mathematics and this keep messing with my head.

What do you mean by “get the direction”? It depends on where your data is coming from and how you want to use it. As he said, a Vector3 is just three numbers, and they are used in many ways: to store the position of a transform, to store the difference in positions, to easily set the rotation (using Euler angles), to store the scale of a transform, and on and on. Some of these ways are easily convertible; some are less straightforward.

If you want to get the direction from Thing A to Thing B, for example, you can simply subtract their positions:

``````Vector3 directionFromAToB = thingB.position - thingA.position;
directionFromAToB.Normalize(); //may or may not be necessary, depending on your intentions
``````

(A while the Vector3 in the first line has the direction, its length - which you could access with .magnitude - will be the same as the distance between the two things. Sometimes you want this, sometimes you don’t. A normalized Vector always has a length of 1, and thus contains ONLY the “direction” part of the vector - it still uses all three numbers to represent this though. Normalizing uses a lot of math, relatively speaking, so only use it if the normalized vector is what you need, especially if you’re doing something that will be done many time per frame.)

Let’s say you want Thing A to start moving towards Thing B, you could use this (use the normalized vector from above):

``````thingA.position = thingA.position + directionFromAToB * Time.deltaTime;
``````

(You multiply by Time.deltaTime to compensate for whatever the framerate may be - it’ll make the thing move more on frames that took longer. The above line will move towards Thing B at one unit per second.)

Or, using physics:

``````thingA.rigidbody.velocity = directionFromAToB;
``````

(obviously requires Thing A have a rigidbody)

Let’s say you want to have Thing A look towards Thing B, you could then use that direction vector like so:

``````thingA.rotation = Quaternion.LookRotation(directionFromAToB);
``````

(You could use thingA.LookAt(thingB.position) to do this, but this is a better demonstration. It also gives you a bit more control over the process.)

Thank you so much for the detailed reply. Now it is crystal clear.

Axis components and direction/magnitude are just two different ways to represent the same information. For example a 2D vector could have an x axis component of 1 unit and a y axis component of 1 unit. A little math will tell you that the magnitude is 1.4 units (the diagonal) and the direction is 45 degrees from the x axis. The same with a 3D vector, although working out the direction is a little more complicated.

What such vector values are used to represent is of course context-dependent. You could simply use the class (or struct, actually) to store three values.

The Vector3D in Unity does actually have a magnitude property, which presumably is calculated from the x, y and z components the way I just did for the 2D vector.