Hey, thanks for checking out my question. I am trying to do gun recoil with my camera so I am using quaternion.lerp, (not slerp because I want it to be linear), and when the player shoots I have the camera lerp to a quaternion that is the cameras local rotation being multiplied by a recoil value. This seems to work fine but I want the camera to come back to the rotation in which the player started shooting. So I also store the cameras local rotation when the player starts shooting, and then when they are done shooting I call a function to lerp the camera back to its initial rotation. I am having a problem where there seems to be an offset in which the camera is stopping 0.4 units before the initial rotation. This offset seems to be inconsistent as well but its around 0.4 units every time. Not sure why it would not be the same value every time or why it stops short in the first place. Ive been messing with it for a week now and still have not found what I am doing wrong. I will provide my code down below and thank you for the help in advance!
void Update () {
if (Input.GetKeyUp(KeyCode.Mouse0) && timeShooting > 0 || bulletsInMag <= 0 && Input.GetKeyUp(KeyCode.Mouse0)) {
StartCoroutine(RecoverRecoil());
}
if (Input.GetKeyDown(KeyCode.Mouse0)) {
initialShotRot = cam.transform.localRotation;
recoilFinishRot = initialShotRot * Quaternion.Euler(maxRecoil, 0, 0);
}
}
//Technically does not need to be in a Ienumerator currently
IEnumerator recoiling () {
timeShooting += 1;
float recoil = 1f;
while (recoil > 0) {
cam.transform.localRotation = Quaternion.Lerp(cam.transform.localRotation, recoilFinishRot, 0.1f);
recoil -= 1f;
if (Input.GetKeyUp(KeyCode.Mouse0) || bulletsInMag <= 0) {
yield break;
}
yield return null;
}
}
IEnumerator RecoverRecoil() {
while (timeShooting > 0f) {
cam.transform.localRotation = Quaternion.Lerp(cam.transform.localRotation, initialShotRot , 1f); //its 1 for testing purposes
timeShooting -= 0.25f;
yield return null;
}
timeShooting = 0;
}