Smooth No Gimbal Lock Analogue Rotation
I'd like to get the values of my controller's left analogue stick. C#
float lh = Input.GetAxis("leftHorizontal") * Time.deltaTime * moveSpeed;
float lv = Input.GetAxis("leftVertical") * Time.deltaTime * moveSpeed;
Then, I'd like to put apply those values to do a Quaternion.LookRotation or some alternative. For some reason, I get snapping to avoid gimbal lock on lots of other methods I've found on the forum.
My current code for applying the rotation:
Vector3 NextDir = new Vector3(-lv, -lh, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(NextDir), Time.deltaTime * 10);
It doesn't work. The movement is smooth, but when you get to a certain point the object flips along a different axis. I only want rotation around the X axis.
TL;DR I want smooth rotation around the X axis with no Gimbal Lock. (Rotate to point in same direction as analogue stick)
Answer by OrbitalTech · Dec 02, 2017 at 10:01 AM
Setting my sensitivity to 1 instead of 3 in the input manager did the trick. For reference, this is my current code. I moved my camera and adjusted the angle.
Vector3 newDirection = new Vector3(lh, 0, -lv);
Quaternion newRot = Quaternion.LookRotation(newDirection);
transform.rotation = Quaternion.Slerp(transform.rotation, newRot, Time.deltaTime * 10);
Your answer
Follow this Question
Related Questions
Controller Stick Input "Snaps" 0 Answers
How can I update UI Text score using PlayerPrefs.Getint() on startup? 1 Answer
HID controller 2 Answers
fps script not working 1 Answer