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.
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
transform.LookAt(facePos);
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.