- Home /
Quaternion slerp not fully rotating
Hey thanks for checking out my question, I am trying to do some gun recoil where the camera will rotate up when the gun is firing, and then rotate back to the original rotation it had before firing. I have it almost working but the camera will not go fully back to the original rotation. Instead the camera stops about 0.8 units away from the original rotation. I do not know why this is but I would appreciate the help.
within FixedUpdate
if (Input.GetKey (KeyCode.Mouse0)) {
isShooting = true;
shoot(); //recoil is called in this function
}
if (Input.GetKeyDown(KeyCode.Mouse0)) {
initialShotRot = cam.transform.localRotation;
recoilFinishRot = initialShotRot * Quaternion.Euler(maxRecoil, 0, 0);
}
if (Input.GetKeyUp(KeyCode.Mouse0)) {
isShooting = false;
StartCoroutine(RecoverRecoil());
}
}
IEnumerator recoiling () {
if (cam.transform.rotation.x == initialShotRot.x) {
yield break;
}
while (isShooting) {
cam.transform.localRotation = Quaternion.Slerp (cam.transform.localRotation, recoilFinishRot, Time.fixedDeltaTime * recoilSpeed);
yield return null;
}
}
IEnumerator RecoverRecoil() {
while (!isShooting) {
if (cam.transform.localRotation == initialShotRot) {
yield break;
}
cam.transform.localRotation = Quaternion.Slerp(cam.transform.localRotation, initialShotRot, Time.fixedDeltaTime * recoilSpeed);
cam.transform.localRotation = Quaternion.Euler(new Vector3(cam.transform.rotation.eulerAngles.x, 0f, 0f));
yield return null;
}
}
Answer by seandolan · Feb 27, 2018 at 04:53 AM
Have you tried RotateTowards instead? This will rotate towards your destination rotation then stop when it reaches it.
https://docs.unity3d.com/ScriptReference/Vector3.RotateTowards.html
Your answer
Follow this Question
Related Questions
Rotating about and only one axis 1 Answer
How to get Angle float of specific axis.(Turret clamping related). 2 Answers
Quaternion Rotation Smooth 1 Answer
Quaternion.Slerp 1 Answer
Interpolate rotation of character. 0 Answers