Search code examples
c#coordinatesunity-game-enginemeasure

Unity: What is Unity Measurements?


What is Unity Coordinates Measurement? (cm, m, km)

How to Implement real measures in it?

I want to implement a real measures in Unity. how to do it?

public Vector2 GetAsMeters (Vector2 vec) {
    Vector2 meters = new Vector2 (0, 0);
    while (vec.x > 1000) {
        vec.x -= 1000;
        meters.x++;
    }
    meters.x += vec.x;
    while (vec.y > 1000) {
        vec.y -= 1000;
        meters.y++;
    }
    meters.y += vec.y;
    return meters;
}

EDIT: How to implement a measurement like "Left 4 Dead"? (this game has a huge map and its as long as real kilometers)


Solution

  • In Unity, it's up to the developer to decide what a unit means. It's quite common that developers stick to 1 unit simply being 1 meter.

    If you do that, you can calculate the length of a Vector2, in meters, by simply retrieving the magnitude of it.

    float distanceInMeters = myVector2.magnitude
    

    By sticking to 1 unit being 1 meter you can also measure the distance between two positions like this:

    float distanceInMeters = Vector2.Distance(positionA, positionB)