Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Touble with Vectors in 2D

Discussion in '2D' started by pzoghbi, Oct 8, 2019.

  1. pzoghbi


    Oct 8, 2019
    Greetings, all. This is my first post.
    Hello all. I take this as a chance to introduce myself to the community.
    Very concisely, Phillip, 26, a degree in the game development, computer engineer, started Unity 5 months back. I'm looking forward to meeting lots and lots of cool guys and girls to exchange ideas and become friends.

    I'm making a game in Unity, 2D. I'm having trouble with understanding Vectors.

    I have three points in the world. A: Player, B: Player Gun, C: Point of the mouse location
    I get these are all represented by vectors.

    The expected:
    I click somewhere on the screen (C) to spawn a projectile at location of a gun (B) with direction from player to mouse location (A).

    The Problem:
    I did some debuggin' and projectile's vector of direction says (-9, 5.5). Assumably, the projectile would move in UP and LEFT direction. But, it goes down left.. what looks like ~(-9i, -1j)

    What am I doing wrong, and how can I help you help me?
    All answers welcome.
  2. Ledesma099


    May 15, 2018
    Hi Phillip, sorry for respond now.

    You can put the code to see if there something wrong?
  3. jeffreyschoch


    Jan 21, 2015
    Hello, welcome to the forums.

    There's lots of resources on the internet for learning about Vector Math, including on Unity's website.
    I'm particularly bored at the moment, so I'm going to explain some stuff about vectors and hopefully it helps you.

    It's important to understand the context of each vector, because that determines its meaning, and where/what its origin (0,0,0) is. Context is a vector's "space" or "units".
    For instance, point C would start off as a point in screenspace, as a Vector2 pixel coordinate on the screen. Pixels being the unit here.

    Points A and B are both positions in worldspace, as Vector3 game world unit coordinates.

    To get meaningful results with worldspace results, both points need to be in worldspace. So first convert point C to worldspace with a Camera function "ScreenToWorldPoint", and then you can find the worldspace vector between A or B and C.

    The math is easy for that:
    Vector2 fromGunToMouse = mousePosition - gunPosition;
    The result is a Vector3 position relative to the gun in worldspace, a vector with the origin at the gun, and its endpoint at the mouse position in worldspace. It has the direction for the projectile to use, it also has the distance to the click in the length of the vector. By making "fromGunToMouse" a Vector2 type, the Z is discarded from the resulting vector, because we only care about 2D in this case.

    Setting the projectile's X axis to align with that direction is easy:
    projectile.transform.right = fromGunToMouse.normalized;
    A purely directional vector is a "unit vector", or a vector that is 1 unit long. Normalizing a vector shortens it to 1 unit, which still retains the original direction. In this specific case, you actually don't need to normalize it, because the Transform class will normalize it for you.

    Unit vectors are very useful in vector math, for example "transform.right" is a unit vector which represents the direction of the X axis of the transform. It is 1 unit long.

    To find a position 50 units to the right of the transform you can do:
    Vector2 position = transform.right * 50f;
    If the direction wasn't normalized, that multiplication would result in a much farther position instead of exactly 50 units away.
    pzoghbi and Ledesma099 like this.