Why Is This Not Rotating By Two Degrees?

Can anyone help explain to me why this is not rotating forward and back by two degrees?

I have the following ienumerator which is supposed to open the jaw on a character, I don’t want to animate it because it’s part of a lip sync script.

IEnumerator OpenJaw(float amount, float speed){
  float start = jawbone.eulerAngles.z;
  float end = jawbone.eulerAngles.z + amount;
  float weight = 0f;
  float startTime = Time.time;
  Debug.Log("Amount is " + amount);
  Debug.Log("Original rotation is " + start);
  Debug.Log("Target rotation is " + end);
  while (Time.time - startTime < speed) {
    weight = Mathf.Lerp (start, end, (Time.time - startTime) / speed);
    jawbone.Rotate(0,0,weight);
    yield return null;
  }
}

It spins all the way around instead of just moving as expected, the readout is as follows.

When going down:

Amount is -2.1
Original rotation is 176.8024
Target rotation is 174.7023

When going up:

Amount is 2.1
Original rotation is 213.8699
Target rotation is 215.9699

Remember .Rotate() is to change it BY some amount, not change it TO some amount.

Also, what values are coming out of Math.Lerp()? I’m not sure you’re using that properly… but maybe?

If you’re looking to change it TO a rotation, create a new rotation with:

jawbone.localRotation = Quaternion.Euler( 0, 0, angle);

Solved using the following:

IEnumerator OpenJaw(float amount, float speed){
  Quaternion start = jawbone.localRotation;
  Quaternion end = startAngle * Quaternion.Euler (0, 0, amount);
  float startTime = Time.time;
  while (Time.time - startTime < speed) {
    jawbone.localRotation = Quaternion.Lerp(start, end, (Time.time - startTime) / speed);
    yield return null;
  }
  jawbone.localRotation = end;
}

Thanks for pointing me in the right direction

1 Like