Using GetAxis("...") for making 180 degree controls

Hi there Uniteers, I am in need of your guidance with some player controls for a third person game.

I am in the process of creating a character control script, which can rotate a character around himself 180 degrees depending on the values from

GetAxis("Horizontal")[“W” and “S”] and GetAxis("Vertical")[“A” and “D”].

The idea with this, is when the user right click his mouse, the camera will be “locked” on the character, and the character will look straight forward (0 rotation) upon right clicking. The player is then capable of rotating his character with the WASD keys. Depending on what key(s) are pushed he should rotate towards that corresponding direction.

W: 0

W+A: -45               W+D: 45

A:-90                                      D: 90

S+A: -135               S+D: 135

S:180/-180

The “Compass” above is how I would like the system to look like. When the “amount of degree” have been found, it will be fed to transform.RotateAround(transform.position, transform.up, rotateFloat), and the character should look in that direction. I have made sure even after the rotation, the compass is still the same.

The main issue with this problem is that I can’t figure out the mathmathical design for this approach using the GetAxis components. Somehow the ranging values (0…1) from both Axis should be able to give me the rotating degree. Some pseudo code:

rotateFloat = Input.GetAxis("Horizontal")*someMath + Input.GetAxis("Vertical")*someMath

There is the option of simply hard coding every state, but I am not very fond of hard coding, especially not controls.

I hope some of you are capable of helping me. Feel free to ask more questions, or if you wish me to make myself more clear.

What you want is something similar to this, which I took from one of Unity’s tutorials. transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (movement.direction), Time.deltaTime * movement.rotationSmoothing);

This moves the character over to some direction, over the rotationSmoothing time. You won’t need Quaternion.LookRotation, but you will need the Slerp.

That’s when you realize, trigonometry is a beautiful thing. You have two inputs between -1 and 1, and you want an angle. It’s like trigo was made for that question. Here is a way I can think of :

First, make sure to round the inputs so you have only three values possible, -1 0 and 1.

Then, calculate the angle from the x axis with Acos, you’ll get PI, PI / 2 or 0. for y, use Asin and get 3PI / 2, 0 or PI / 2.

Finally, calculate the average, convert to degree, and there you go !