Rotating caps, how could I be more precise ?

Hi guys,

I am trying to rotate a cylinder http://puu.sh/8khwR.jpg on his Z axis.

I would like it, when I push “E”, to rotate on the right (-60 degrees), and to rotate on the left (60 degrees) when I push “A”.

I am actually changing a boolean, changing a target with +/- 60 degrees on his Z (with eulerAngles), doing the translation unto the target, then changing the boolean again to stop the movement.

Problem is that the target, when I click one more time, take 0.5 degrees more : http://puu.sh/8khKK.png

Moreover, I got another problem, when I give -60 degrees, it does an entire movement on the left to reach 300 degrees, not a little movement on the right to reach -60…

Puush of my code : http://puu.sh/8khPI.png

Thanks,
Regards

Please post code as code, not as a screenshot. In any case, I’d suggest using Lerp, for example.

–Eric

You mean like so?

transform.Rotate (Vector3.up, 60, Space.World);

Hey, sorry for the code.

I tried to use a simple Rotate, but it doesn’t move smoothly, which is not what I want. With a Lerp, the movement is done but again, it comes to 60.5. The first movement goes to 59.99999… then, when I reclick, it goes to 60.5.

(screenshot of the inspector http://puu.sh/8knSw.png)

If you use Lerp, the end rotation is whatever you specify.

–Eric

I understand.

I actually end up with something almost good. The value never reach 60 since it depends of Time.deltaTime.

I changed the way I compared the two values, I increased the range with a “-0.0001”.

I still got two problems :
Doing such a delay of values could end to a big delay doing it a long time, right ?
And, how could I rotate in a negative way without doing the whole round of the circle ?

I believe you are looking for MoveTowards.

Edit:

What I originally mentioned is:

    transform.Rotate (Vector3.up, 60, Space.World);

That is your end rotation. it is best that you have a second object that is the same space as your object. Every update, you reposition that object to the location of your actual object, check for any keys that are pressed, rotate that object using transform.Rotate (Vector3.up, amount, Space.World);

Then you simply keep lerping, or moving towards the rotation of the other object. It never rotates the 360 back because none of this is dependent on euler angles. :wink:

Well, it works nicely with this trick :slight_smile:
I now only need to create booleans to control those movements and not have a lot of them.

Using a second empty gameobject is something I don’t think of, but that’s really usefull to be honest !
Thank you guys :wink:

What you probably want to do is, in a coroutine, first generate your “final” rotation, then use Quaternion.Slerp to rotate you there. Here’s a basic example of that:

...
   StartCoroutine(RotateBy(60f));
...

IEnumerator RotateBy(float angle) {
   Quaternion startRotation = transform.rotation;
   Quaternion endRotation = startRotation * Quaternion.AngleAxis(angle, transform.forward);

   float duration = 1f;
   for (float t=0f;t<duration;t+=Time.deltaTime) {
      transform.rotation = Quaternion.Slerp(startRotation, endRotation, t/duration);
      yield return null;
   }
   transform.rotation = endRotation;
}