I’ve been experimenting with AI Controllers and my Unreal Guys walking towards random locations in the game world. As such I needed a way for them to turn smoothly towards said random locations before they starting to walk. Thankfully I found out about the Find Look At Rotation node. It was exactly the missing piece to this brainteaser.
Here’s how I made my Unreal Guys turn towards a new location:
This is a custom event on my character. It’s called from my custom Behaviour Tree Node, passing in a new Vector called New Location. First I’ll grab my capsule component’s World Location and use the Find Look At Rotation node to get where (or what) my character should be looking (at). This node needs the new location to give us a Rotation value. I’ll break that struct and access the Z rotation.
I’ll also grab my current World Rotation and find out where my character is currently looking at. Again the Z rotation is all I need to proceed. We need to find a way to interpolate between these two values, then continuously update the Z rotation until we reach the new value. At that point, we’re looking towards where we’re going to walk next.
To interpolate these values, I’m using a combination of Timeline and Lerp Node. The Timeline will give us a float value between 0 and 1 interpolated over one second. When plugged into the Lerp Node, it’ll interpolate between two values of the same kind and give out the interpolated value (in our case, the Z rotation). We’ll use that to make ourselves a new Rotator, which will then update our current World Rotation.
Lerp nodes are very flexible. It stands to reason that the breaking/making rotations could be avoided, since the Lerp can deal with blending two rotators. I’ve tried this too, but it leads to weird results on the characters. I’d advise against it. It would make for a neater Blueprint though – check it out:
On a personal note, I was quite surprised that with my limited knowledge of Unreal Engine, I managed to work out this rather complex equation. I’m beginning to understand how Unreal Engine thinks 🥰