# Tile Map [Suggest] Add `Vector3Int.ManhattanDistance(Vector3Int a, Vector3Int b)`.

Discussion in '2D Experimental Preview' started by RyotaMurohoshi, Dec 6, 2016.

1. ### RyotaMurohoshi

Joined:
Jun 20, 2013
Posts:
21
I want methods to calculate Manhattan distance with `Vector3Int`s.

There is a method to calculete Euclidean distance (`Vector3.Distance(Vector3 a, Vector3 b)`, but sometime we need the method to calculate Manhattan distance in tile based game logic.

https://docs.unity3d.com/ja/current/ScriptReference/Vector3.Distance.html

I think many of us creted the method to calculate Manhattan distance.

I suggest to create static methods in Vector3Int

Code (CSharp):
1. // in Vector3Int
2. public static int ManhattanDistance(Vector3Int a, Vector3Int b){
3.         return Mathf.Abs(a.x - b.x) + Mathf.Abs(a.y - b.y) + Mathf.Abs(a.z - b.z);
4.     }
5. }
and with Vector2Int

Code (CSharp):
1. // in Vector2Int
2. public static int ManhattanDistance(Vector2Int a, Vector2Int b){
3.         return Mathf.Abs(a.x - b.x) + Mathf.Abs(a.y - b.y);
4.     }
5. }
update : added checked

Last edited: Dec 6, 2016
idbrii, mgear and S-Takahashi158 like this.
2. ### Sdk_fn

Joined:
Sep 29, 2015
Posts:
4
Thanks men is wonderfull code.

Greetings from Colombia

3. ### Invertex

Joined:
Nov 7, 2013
Posts:
1,121
You can add your own static extensions of Vector3Int to do this as well for better ease of use, though they definitely should implement it internally.

Code (CSharp):
1. // in Vector2Int
2. public static int ManhattanDistance(this Vector2Int a, Vector2Int b){
3.         return Mathf.Abs(a.x - b.x) + Mathf.Abs(a.y - b.y);
4.     }
5. }
And then your code simply becomes
``position1.ManhattanDistance(position2);``

Q-Ted likes this. unityunity