Help with rotate character smoothly

Hey everyone, I have a questions for people who are good at math…

So I have this simple rotation overtime of my character as shown in the SS below.

My question: Sometimes the player rotates the other way around itself to reach the rotation instead of finding the shortest rotation distance. How do I make it so the character only rotates the shortest amount possible. For example, if two look at points are next to each other, the character doesn not need to spin around itself to look at the other position, he could just spin the other way instead and would look more realistic.

Thanks guys!

The are two problems here.

One is, that the engine likes to jump from -180 to 180 or the other way around, without warning. You can get around this, by either using Quaternions, or on only adding and subtracting local rotation.

Problem two is your code ( sorry :slight_smile: ) With timelines and lerps, you need to calculate the start and end points before going into the timeline. You’re recalculating them during the timeline, which will lead to all sorts of weird shenanigans.

1 Like

Hey man, thanks for the quick reply!

If you have the time, would you mind showing me a fix for this? I’m not quite sure where to start.

Sorry and thanks again.

1 Like

Put your current and target rotation in variables. Then, go into the timeline and use the lerp to rotate the actor.

Tell me if you don’t get it :wink:

1 Like

Great thanks, but it didn’t fix it sadly, the character still does a full spin sometimes.

1 Like

Ah, then it is the lookat rotation node.

I had exactly the same thing with it.

Try it like this, I can’t get it to spin:

lookat

4 Likes

Works great man, thanks!

For other people that might be wondering, it does not need to be on the event tick to work, if you set the focus of the AI to the actor in the behavior tree afterwards.

Have a good day!

2 Likes

Thank you, I was freaking out imagining all of the noodles to fix this problem. Your flair is well-deserved.

1 Like