Unity3d/C#: Check Distance

Similar to ‘Check Within Distance’, only that this time it will actually return the distance value and this distance will be returned as a ‘Vector3’.

public static Vector3 CheckDist(GameObject con1, GameObject con2)
    {
        var pos1 = con1.transform.position;
        var pos2 = con2.transform.position;

        var posx = pos1.x – pos2.x;
        var posy = pos1.y – pos2.y;
        var posz = pos1.z – pos2.z;

        Vector3 distance = new Vector3(posx, posy, posz);
       return distance;
    }

Con1 = First Object to compare

Con2 = Second Object to compare

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s