- Home /
Slerp doesnt go to the rotation its supposed to go to
Hello! So, im making an FPS and my recoil resets with the slerp method. the original rotation is 90.0001° on the Y axis. But everytime i shoot and it slerps back, it sometimes goes to 88°, then after the other shot it goes to 91.112° etc.
IEnumerator Recoil()
{
if (Input.GetMouseButtonDown(0))
{
isShooting = true;
f += 0.3f;
x = Random.Range(0, 2);
if (x == 0)
{
transform.Rotate(0, 3.5f, 0);
f += 0.1f;
}
if (x != 0)
{
transform.Rotate(0, -3.5f, 0);
f += 0.1f;
}
yield return new WaitForSeconds(0.05f + f);
isShooting = false;
f = -0.3f;
}
}
this is the recoil part.
private bool isShooting = false;
private void Start()
{
hipFire = transform.localRotation;
}
void LateUpdate () {
StartCoroutine(Recoil());
if (!isShooting && transform.localRotation != hipFire)
{
transform.localRotation = Quaternion.Slerp(transform.localRotation, hipFire, 12f * Time.deltaTime);
}
}
and this is the part where the weapon is supposed to slerp. So, is there a way to make the slerp more accurate? Or is slerp like that and theres nothing i can do?
Answer by JVene · Sep 02, 2018 at 02:13 AM
@xNIkurasu I come over questions like this frequently, and I just can't resist this little but of both fun and advice:
In 30+ years of programming I've never seen a computer just refuse to perform a calculation, or ignore some 'if' statement. In my first few years I would have sworn it did, just to spite me. I would have bet all the cash on hand that it was just ignoring my code. It has never once been true.
In this case, the operation of slerp depends on a start and end example (a from and to) that remain stable at every subsequent call to slerp. The idea is that slerp (or lerp or any such function) will interpolate between "from" and "to" according to what is basically a 'percentage' expressed in the range from 0 to 1 (where 1 is 100% of the 'to', and 0 is 100% of the 'from' values).
However, in order for that to work, both 'from' and 'to' must never change from one call to the next. In your code, however, your 'from' value is the moving target of the loca rotation. So, let's take an oversimplified walk through about that.
All lerps and slerps apply similarly, so let's say it's lerp where 'from' is 0, 'to' is 100, and our first call is i = .5. The result for a lerp would be about 50. If i were .1, the result would be about 10. However, let's stay with i = .5, where the lerp would be about 50, but change 'from' to 50. Now, where 'from' is 50 and 'to' is 100, with i = .5, the result would be about 75.
That's what's happening in your code. You must use a 'from' that doesn't change from one cycle to the next, like you did with the 'to' value.
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Unit rotation fails consistently on all slerp rotations after the first? 0 Answers
Multiple Cars not working 1 Answer
Smooth rotation problem 1 Answer
HoloLens - Smooth Rotation doesn't work 0 Answers