Precise rotation based on joystick axis input for twin-stick controls

I’m hoping somebody will throw me a bone here. I’ve searched the fora and haven’t found anything that solves my dilemma.

I have a character controller that gets a Direction Vector based on joystick input, then rotates the character using that Direction Vector:

public Vector3 DirVector { get; set; }

Direction += new Vector3(Input.GetAxis(“Horizontal”), 0, Input.GetAxis(“Vertical”));

transform.rotation = Quaternion.LookRotation( DirVector );

The problem is when ever it comes within 5 degrees of any 90 angle, it snaps to that angle. This means that the player has no control over ~40 degrees of rotation. Now, I understand that Unity does this internally in order to avoid gimbal lock, however I don’t see how gimbal lock can occur if I’m only rotating on the y axis.

So how do I get around this? I know that you can get a character to rotate to a specific angle, using euler angles, that is within the elusive, 5 degree range:

transform.eulerAngles = new Vector3( 0,87,0 );

I guess what I’m asking is, how do I convert a direction vector ie. Vector3(5,0,8); to a y rotation in euler angles? A layman response is preferred since I don’t understand trigonometry. Also, in C# please.

Thanks in advance!

Transform.eulerAngles = new Vector3( 0, Mathf.Atan2( Input.GetAxis(“Vertical”), Input.GetAxis(“Horizontal”)) * 180 / Mathf.PI, 0 );

Should get you pretty close. Might need a negative sign on one or both of the arguments depending which way your camera is aligned, assuming its axis aligned. If not, the math is a bit harder.

Well I know this is old as dirt, buuuut, here’s a solution to make an object face the direction you move a joystick to.

        var _horizontal = Input.GetAxis("Horizontal");
        var _vertical = Input.GetAxis("Vertical");
        // Direction pointing to
        Vector3 newPos = new Vector3(_horizontal , 0, _vertical);
       // Get the current position of object
        var currentPos = _rigidbody.position;

        // Set the current position plus the position targetting
        var facePos = currentPos + newPos;
       // Set it 

well if the camera changes yaw ( i.e. orients around the character ) then you’ll just need to add the camera’s yaw in to the yaw of your character as an offset. so get the camera’s transform.eulerAngles.y ( since yaw is the y component ) and add that in right after the atan2 result. Again, you might need to play around a bit and throw some negative signs in, or a +180 to make everything work, but that’s the basic idea.