When clamping rotation how can I make that it will start from the current rotation ?

private Update()
{ 
  float rotationY = 10 * Mathf.Sin(Time.time * speedRot);
  var clamp = Mathf.Clamp(rotationY, -10, 10);
  transformToMove.localRotation = Quaternion.Euler(0, clamp, 0);
}

The problem is that it’s first rotating by 10 angles to the right and then start rotating smooth slowly between -10 and 10 but I need it to start rotating from it’s current rotation either to the left or to the right and keep making the rotation between the -10 and 10 limit. The way it is now it looks like first time it’s jumping to the -10 or 10.

If I change the Time.time to Time.deltaTime it’s not rotating at all.

Screenshot of the model object structure in the hierarchy :

Parent is just empty GameObject

Both ROOT and Flyer positions are set to 0,0,0

Didn’t we talk about this exact problem yesterday? Time.time is some random value by the time you reach here. Use your own counter and start it at zero, since we know that sine(0) == 0

If you doubt me, then break line 4 into several lines, print the values going into Mathf.Sin() and you’ll see.

1 Like

Working ! Thanks.

At the top I added :

float count = 0.0f;

Then in the Update :

private Update()
{
  if (Mathf.Approximately(transformToMove.localPosition.y, 7))
            {
                toLand = true;

                count += Time.deltaTime;

                float rotationY = 10 * Mathf.Sin(count);
                var clamp = Mathf.Clamp(rotationY, -10, 10);
                transformToMove.localRotation = Quaternion.Euler(0, clamp, 0);
            }
}
1 Like

Very nice… you can probably get rid of line 11 because if Mathf.Sin() ever returns less than -1 or greater than 1, we’re all in a LOT of really deep trouble here in computer land.

1 Like