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;
}
}