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

Do points really exist in unity? (Vectors)

Discussion in 'Scripting' started by Bazz_boyy, Jun 5, 2015.

  1. Bazz_boyy

    Bazz_boyy

    Joined:
    May 22, 2013
    Posts:
    192
    Hi guys,

    I've been really confused with vector maths lately and I can't solve a problem in my game because of it (finding the bearing from one point to another using my own vector3 as err my local space?).

    As the title says, are there really points in unity, or is every vector2/3 a vector whose starting point is 0,0,0 in world space? If so, how does unity differentiate vector2 ( x.position - y.position) (x ->y) and not just see it as (0,0,0 -> x - y). And if this is NOT true, then how how can there be points in unity?

    For instance in Debug.DrawRay function, it takes a "position in world space". How? Is this position actually a vector whose starting points is 0,0,0?

    I just don't understand! Please, someone, enlighten me X'D

    Thanks.
     
  2. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Check this out: http://en.wikipedia.org/wiki/Position_(vector)

    Particularly: is a Euclidean vector that represents the position of a point P in space in relation to an arbitrary reference origin O

    Vectors in Unity can represent points(as above) and directions.

    position in world space means "At this position, offset from Unitys Origin point, which is (0,0,0)".
    position in local space means "At this position, taking the parents world position as it's Origin then offsetting". You can try this yourself by moving a Gameobject to a random position in Unity, then adding a gameobject underneath that and setting it's position to (0,0,0).
     
    Bunny83 and Bazz_boyy like this.
  3. Bazz_boyy

    Bazz_boyy

    Joined:
    May 22, 2013
    Posts:
    192
    I kind of understanding what you mean. Alright, thanks so much for the references! I will study them and hopefully grasp a greater understanding. Thank you so much! I really appreciate it :)
     
  4. Zerot

    Zerot

    Joined:
    Jul 13, 2011
    Posts:
    135
    There is no difference between a point and a direction+magnitude. It is how you use them that makes them "different". Positions you tend to use directly, where as directions are used as intermediate values.

    To maybe explain it a bit better, lets reduce the problem to 1 dimension. When I give you the number 5, it can both mean the position 5, or a positive direction with magnitude 5.

    So lets say that position A = 5, and we have the direction d of 5. To calculate where A would be when moved along the direction d, we can just add the direction to the position. A' = A + d = 10. See how A is moved 5 units in the positive direction?

    So, how do we find the direction between 2 positions? Say we have the positions B and C where B = 10 and C = 15. What would be the direction and magnitude from B to C? e = C - B = 5. But if we want to have the direction from C to B we have to do f = B - C = -5.

    See how in the resulting values there is nothing stored about the start point? We could interpret those directions also as positions, because they are just a number. Better yet, we could say that A' is the same as d in the local space of A. If A is the center of the world (A = 0), then d and A' are the same. In the same way, C and e are the same in B's local space.

    Now lets bring this to 2D. position A = 5,5, direction d = 2,6; A' = A + d = 7,11. See how it works the same as in 1D? the x's and the y's are just added together to move A to A'. Calculting the direction from point B to C also works the same way. B = 3,7, C = 1, 9; e = C - B = -2, 2.

    This math stays the same regardless of how many dimension you use.

    One other thing: You might have encountered the term normalized or unit-vector. A unit-vector is a vector with a magnitude of 1. When you normalize a vector, you make the magnitude 1. So if we take the 1D example again: e = C - B = 5; To normalize that we just divide it by its magnitude. ^e = e / ||e|| = e / 5 = 1. Do note that magnitude is always positive. For example, f = B - C = -5. That -5 is a negative direction with a magnitude of 5. So to normalize it would be ^f = f / ||f|| = f / 5 = -1. See how the direction stays negative?
     
    ThermalFusion likes this.
  5. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Vector3 is simply storage for 3 floats with some functions you can call for useful things. The fact they're called x,y,z doesn't mean anything. You can use it for a position, a direction, or anything you like. This is why it can be confusing to a newcomer, because of the open-endedness of it.
     
  6. Leohd

    Leohd

    Joined:
    Feb 7, 2017
    Posts:
    32
    In Unity, Vector3 represents both points and vectors. Mathematically, points and vectors are distinctly different.