I'm currently working on a twin stick shooter but I encountered a problem when I rotate my player. The rotation snaps to right angles. I'm guessing it's a problem, due to the value that the joystick gives me back. Here is a screen recording, via Giphy.
How do I go about fixing this problem?
This is my code:
void FixedUpdate()
{
float hR = gamepad.GetStick_R().X;
float vR = gamepad.GetStick_R().Y;
float lastHR = hR;
float lastVR = vR;
// This part was added recently in the hope of smoothing the rotation
float lerpHr = Mathf.Lerp(lastHR, hR, Time.deltaTime * speedRotation);
float lerpVr = Mathf.Lerp(lastVR, vR, Time.deltaTime * speedRotation);
PlayerRotation(lerpHr, lerpVr);
}
void PlayerRotation(float h, float v)
{
rotation.Set(h, 0f, v);
if (rotation != Vector3.zero)
{
currentRotation = Quaternion.LookRotation(rotation);
playerRigidbody.MoveRotation(currentRotation);
lastRotation = currentRotation;
}
else
playerRigidbody.MoveRotation(lastRotation);
}
I also tried two other ways, but each time, I got the same results. I tried every way I could think of, with these lines of code. I don't know where else to look.
// Attempt Two
playerRigidbody.transform.rotation = Quaternion.Slerp(p
layerRigidbody.transform.rotation, currentRotation, Time.time * speedRotation);
// Attempt Three
transform.eulerAngles = new Vector3(0, Mathf.Atan2(v, h) * 180 / Mathf.PI, 0);