- Home /
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.
Yeah, I might be able to use some of that code to make the rotation look prettier, but that really doesn't resolve my issue. I was about to tell you that I can't use that piece of code, but taking a second look at it, it might actually be what I am looking for. I'll play around with it :)
$$anonymous$$any, many thanks - Pleas turn your comment into an answer, so I can make it solved, and post my solution :)
Answer by cdrandin · Jan 22, 2013 at 10:03 AM
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.
Answer by Berenger · Jan 22, 2013 at 10:46 AM
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 !
I must say, that could possibly have been a solution. Though cdrandin's solution only involves program$$anonymous$$g rather than math, and my program$$anonymous$$g skills are greater than my math skills :). I am sure if someone else come across this question in need of help, they should look towards this solution as a possible solution.
Your answer
Follow this Question
Related Questions
Mouse Orbit Script with mecanim & turning character 0 Answers
Camera rotates around player but player movement is incorrect 1 Answer
Rotation problem: Check is failing/ endlessly spins 1 Answer
Make 3D Player look exactly at mouse with ScreenToWorldPoint? (maths Question) 2 Answers
Rotate an Object by a spezific value 2 Answers