- Home /
Circular Analog Input?
Hello,
I need a character to face the direction the player is pressing on an analog stick. I know how to get the input axes from a joystick, but no matter how I try to get any angle or anything from them, the input range comes out square. I would presume some trigonometry has something to do with the correct answer. Any ideas?
Thanks!
Comment
Best Answer
Answer by Bunny83 · Oct 29, 2012 at 11:27 PM
// C# or UnityScript(JS)
var direction = new Vector3( Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical") );
transform.forward = direction;
An alternative:
// this gives you more control over the rotation around the look axis if needed
var direction = new Vector3( Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical") );
var rotation = Quaternion.LookRotation(direction, Vector3.up);
transform.rotation = rotation;
ps: You might want to implement a dead-zone. Depending on the type of analog stick it might emit some jitter around it'e home position. The easiest way is something like this:
if(direction.magnitude > 0.1f)
Thanks so much; this, on top of me setting the joystick axes Sensitivity to 1 in the Input $$anonymous$$anager, worked! Glad you could help!