- Home /

# How do I translate a vector2 into one of the fixed directions

Hi, Let's say I have some vector2, based on user input.

How do I find the nearest "direction" for that vector?

I would like to get if it's closest to Vector2.left of Vector2.up, as an example.

Is there a trick to this in unity, or do I need to dive into some vector maths?

**Answer** by rufopufo
·
Jul 15, 2019 at 10:52 AM

Hi there,

Firsty, you will need to see to what axis, is your vector nearer. You can achieve this, by simply comparing x and y components of your vector in absolute mode.

Example: Vector (0.3,0.7), has a greater y, so the Y axis, will be the nearest.

Vector (-0.4,0.1), has a greater x in absolute mode, so the X axis, will be the nearest.

Having this, you can handle the direction by observing the sign of your vector component.

Example: (-0.4,0.1), we had determine that X axis is the nearest, and the x component is negative. So Vector2.left will be the desired direction.

(0.3,0.7), we had determine that Y axis was the nearest, and 0.7 is positive, so the direction is upwards.

You don't have to really dive into maths, just compare the components of your vector.

Hope it helps. If you need more help, just comment.

Thanks! It's so simple, I guess I was blinded by my own math-failures.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Vector rotation 1 Answer

Rb2D.MovePosition not working - weird error... 1 Answer

Switch force from horizontal to vertical 1 Answer

Having difficulty with Lerp 3 Answers

Multiplying Vector2 from another script. 2 Answers