Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in '2D' started by Pucci86, Apr 27, 2015.

  1. Pucci86

    Pucci86

    Joined:
    Apr 14, 2015
    Posts:
    9
    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.
     
  2. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    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.
     
  3. Pucci86

    Pucci86

    Joined:
    Apr 14, 2015
    Posts:
    9
    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.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    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:
    Code (csharp):
    1. Vector3 directionFromAToB = thingB.position - thingA.position;
    2. 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):
    Code (csharp):
    1. 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:
    Code (csharp):
    1. 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:
    Code (csharp):
    1. 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.)
     
    Last edited: Apr 27, 2015
  5. Pucci86

    Pucci86

    Joined:
    Apr 14, 2015
    Posts:
    9
    Thank you so much for the detailed reply. Now it is crystal clear.
     
  6. christinanorwood

    christinanorwood

    Joined:
    Aug 9, 2013
    Posts:
    402
    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.