# How to properly handle Vertical&Horizontal Input

Hi all,

I have recently stumbled upon a problem that I can´t think of a solution.

I am working with Unity 2D and try to achieve 8 Direction Shooting at calculated angles through euler.angle

Here´s an example of the desired result:

As of now, I´m able to fire my projectiles up, down, left and right.

Stuff is handled with lookingRight and lookingUp bools, to determine which euler.angle is going to execute.

And here comes my problem… Since I want to be able to shoot diagonally (inluding Controllersupport like Xbox 360 and such)
I am entering 2 Inputs at the same time (Y,X axis on controller in north east) The script doesn´t recognize this action though and ignores it.

Maybe someone can help push me in the right direction?

You can use the Mathf.Atan2() function to put horizontal and vertical offsets in and get an angle out, from -pi to pi around the circle.

Using that you do more things:

• scale it up to degrees using Mathf.Rad2Deg so you can feed it back into Quaternion.Euler() type functions
• quantize it so that it only comes out on (in your case) 8 different directions.

Quantizing generally involves dividing by a quantity, taking the integer of that amount, then multiplying by that property again.

This breaks down around the zero mark, since multiple + and - numbers map to zero, and that range is larger than one of your quanta.

Therefore you should ensure the numbers are positive, or do specific checks.

In the case of Mathf.Atan2() output multiplied by Mathf.Rad2Deg, the number will be -180 to 180.

In the case of wanting 45 degree quanta, you need to offset it by 22.5 degrees so they’re centered.

Finally, what you do is divide by the quanta size, take the integer, expand back up by the quanta size, presto, you have an angle constrained to certain degrees.

I put together a little script and package to basically capture the above steps. See attached.

1 Like

That was waayy more than I expected. Almost cleared up everything, that boggled my mind.

You have some cool games out there. Keep em´ coming pls

Thanks a lot Kurt!

1 Like