Download

The AI won't turn to face something behind it

Hey everyone, got something here that’s probably a simple solution but its been giving me trouble for awhile now (who knows, typing this out may help me figure it out:

First off here is my earlier thread on the Answer Hub, different issue but it provides more info as to how this is set up:
https://answers.unrealengine.com/questions/391671/how-can-i-give-smooth-rotation-to-a-strafing-ai.html

So currently my AI works pretty well except for one thing, its rotation can jutter, stutter, and jump. To fix this I used RInterp to set the Actor’s rotation to face the player using Find Look At Rotation. This somewhat worked, but it never quite did it.

The main issue though, is the AI will not turn to face the player if the player can get behind it.
My gut tells me this is more to do with the Behavior Tree, as it will face the player once they leave the “acceptable distance” of attacking (300 units between the AI and the player).
But I also don’t know if this has to do with its rotation, as it will rotate to the player if I set the interp speed to something ridiculous. At 3 or 5 though it very, very slowly turns to the player. Not smoothly, mind you.

Anyway, pictures are way more help than words so:

First, we have the Behavior Tree service, Data Check. This runs each tick and sets a bunch of values the AI uses. One of these is the AI’s rotation as seen here:

Rotation_Troubleshooting_Stitched.jpg

It switches between using the Controller Focus node and the Set AI Rotation Interface as Focus is the only one to give smooth movement during strafing behavior (as talked about in the Answer Hub post).
Yes, its hacky and I’m trying to find a better solution.

Next, we have the rotation function itself inside the AI actor’s event graph:

Rotation_Troubleshooting_3.jpg

Setting Current Rotation again rather than passing the value through is probably unnecessary, but anyway.

Next we have what the issue looks like inside the editor:

157d64bb05d1ef15f5184470fa656b44e12b6b0c.jpeg

Now while behind the AI, the rotation will either not work or move so slow as to not be working at all. The AI will continue to swing at air as long as I stand right here. If I leave its acceptable radius and it needs to get into range again by moving it will rotate towards me.
But while standing there, nada.

One last thing, I decided to set up a key for if the AI is facing the player or not. If not, it can’t attack. To test this I went and stood directly in front of the AI.
Well…

6af964355f630a10f75ed2fbb0a8fe30999185a2.jpeg

So now it never attacks even when facing the player directly.

Scoured over other threads trying to find something similar but I think this might be unique to me and lies more in the Behavior Tree than the RInterp node. Or in one of the images I’m using incorrect logic and need a nice smack on the head to set me straight.
Or code goblins are changing my values when I’m asleep, who knows.

In Summary:

  • The AI does not rotate smoothly using RInterp.
  • The AI does not rotate to face the player if the player is behind the AI and within its Acceptable Distance/Radius of 300 units (From the AI to the player).
  • If the player leaves this distance/radius, the AI will move towards them and end up rotating to face them.
  • The function in the Behavior Tree Service shows that with the way I have it set up, the AI never sets “Facing Player” as true when the player is in front of the AI.
  • I’m probably doing something obviously stupid in the above images, please let me know if so.

Other additional info that may help:

Orient Rotation To Movement: Set as True

Use Controller Rotation Yaw: Set as False unless Strafing.

Thanks in advance for any help!

How comes your not using PawnSensing component on AI that will give a forward Arc as well if you do sound for footsteps on player you can use the Sound sensing as well in that component…
or even use a linetrace to remember where he is and then set conditions ie facing player… or maybe a % chance of him phasing the same logic from behind…

Nice work so far though

Turn off orient rotation to movement, that’s going to conflict with any manual rotation modification. But you won’t want to manually rotate. You’ll want another boolean, ‘Use Controller Desired Rotation’ to be true. You can adjust the rotation rate to change the rotation speed. You also need the nodes ‘Set Focus’ and ‘Clear Focus’. These are for the AIController. If you set the focus to be the player, the AI will rotate to face the player. Clear focus to stop this. I actually just re-read your post, and although you are already using the focus nodes, I think you should play with them more. Maybe update the rotation speed based on if they’re strafing or not.

As for ‘isFacing’… I think your nearly equal tolerance needs to be increased. That’s a fairly low value, especially for checking if the player is inside a sword weapon arc.

I never have gotten much into pawn senses yet, I fiddled around with them previously but set it aside while I got the rest of the AI finished up and running. It’s something I’ll definitely look into once everything is stable. Sight line and sound are gonna be fun to play with as I’m just using a sphere trace/line trace combo for their sight at the moment.

Thanks by the way!

Good call on the isFacing issue, I adjusted it up to 25 and it works splendidly with a sword. With the ranged weapon I can play with that more so it’ll quit shooting over my shoulder.

Rotation_Solution_1.jpg

(Picture for anyone who comes looking for help with a similar issue and who happens to like pictures)

And you were right, as was the guy in the Answer Hub post. Focus is the way to go, I was just implementing it incorrectly.

How I was using it incorrectly for anyone coming here to see why their stuff is borked:
Issue was with Orientation Rotation To Movement, it was fighting with the controller focus as it turns out they can’t be used together (obvious in hindsight…and there’s a tooltip that says so in the editor).
Solution:

0e2938e0df1bdb26fd273468b00aba4552f7881d.jpeg
(picture for the picture lovers)

Gotta go into CharacterMovement, switch off Orientation To Movement, switch on the one that uses the Controller’s desired rotation. Completely ignore the RInterp stuff I posted above and use the Set Focus node instead.
It handles that stuff by itself quite well.

Result, now it rotates to face the player within the correct tolerance range and it turns to face the player behind them. All it needs is a bit of nitpick work such as playing the right animation for turning around and adjusting the turn rate (if that’s possible, should be).

Might post a small video of it in action, gonna run some tests with Strafing to ensure all is well.

Thanks for the help Korkun!

Cheers, Metalbreaker. Very helpful follow-up/solution.