RotateTowards with specific direction

Hi All,

I am having real issues with rotations. I’ve got a 2.5D game so my character only faces left or right. I turn the character over time (not instantly). My current code works wonderfully, but I need to be able to choose the direction (clockwise / anticlockwise) in which he rotates. Here’s what I’m currently doing:

My rotateAngle is a Vector3 of either (0,0,0) = Right or (0,180,0) = Left.

I test if the character has reached the new angle by:
if (Quaternion.Angle(targetTransform.rotation, Quaternion.Euler(rotateAngle)) <= 0.01f)

If I’m not at the desired angle I rotate towards it with:
targetTransform.rotation = Quaternion.RotateTowards(targetTransform.rotation, Quaternion.Euler(rotateAngle), speed * Time.deltaTime);

This all works great but my character always rotates away from the camera which I want for some animations but not for others. Any advice on how I can control the rotational direction would be massively appreciated!

Thanks!!

@HappyKat78

How about first rotating towards the halfway direction (facing correct direction) then rotate to your target direction?

The problem is that RotateTowards make it as fast as possible, but in case target rotation is 180 deg it could make it from two sides, but only one can be picked.

You can rotate it just a tiny bit towards right direction when you start, so you force RotateTowards to pick the direction you want or you can make your own rotation method.

1 Like

Thanks both. I tried rotating a little to start so it would pick the shortest direction but I didn’t seem to work for some reason. I’ll trying rotating to half way as a set angle and then rotating again for there. Thanks

@HappyKat78

Did you try values like these:

leftRot = Quaternion.Euler(0, -90.1f, 0);
rightRot = Quaternion.Euler(0, 90.1f, 0);

I actually got this to work by offsetting the initial rotation by 1 degree in the direction I wanted to turn (so either 1 or 359) before I started the rotation. I had tried that before but was trying to offset the turn back from left to right around the 180 (179 and 181) but realized that regardless of direction I needed to offset from 0.

Thanks for the responses, it helped!! Hopefully this helps others in the future too.

If you only spin around the y-axis, using simply int ySpin; can give more control. Hand move it from 0 to 180 and so on, and use Quaternion.Euler(0,ySpin,0) for the final rotation.

The nice thing about RotateTowards is the way it understands 3D and doesn’t care about axises. It can make a perfect diagonal move from facing A to B, which would be almost impossible with x,y,z.

1 Like

If you set your direction with Quaternion.LookRotation, you can get a nice 2 line solution.
The docs state this:

The key to the rotation direction can be set offsetting that “positive Z-axis (0,0,1)” with a very small negative number. So for rotating towards the camera in my case for a 2.5D game, I set a new Vector3’s Z-axis to -0.001f and get the desired rotation. You can set it to 0 to rotate away from the camera.

Quaternion toRot = Quaternion.LookRotation(new Vector3(MoveInput, 0, -0.001f), Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRot, RotationSpeed * Time.deltaTime);

Hope that helps someone.

It won’t help for long because quaternion is being depreciated to legacy in the next major version of unity.

What is “legacy”? Do you have a link to this information? This is news to me, did you make this up? :wink:

3 Likes