How to make an AI Tank Turret follow the player? Whilst the tank is moving?

I have a pawn tank made with a mesh chassis asset and a turret asset in a capsule. The tank’s alert state actives and it follows the player just fine.

However, the turret is supposed to follow the player. It instead starts glitching out pointing in the air and in random directions as the tank moves, I’m updating the turret’s location using a tick event and a “Find Look at Rotation” node.

What am I missing? or do I need to fix to have the tank move and it’s turret also follows the player?

What works: The Tank is alerted when the player is close and starts moving.
What I need to fix: The tank’s turret does not rotate to point at the player when alerted (it glitches out).

Any help would be appreciated!

You can set it up using relative rotation and world location if the turret is a component in the tank’s BP like this:

Or using just world rotation like this:

@Black Phoenyx Thank you for your reply. I’m trying to figure out the best place that this code would work. This BP set up would be located in the Tank’s BP script? or would it be best suited to place this into a parent script or it’s own function?

Is there a better way to optimise it instead of usinig tick? I’m wondering whether this would work with timers and being solely event driven. But I’m not an expert in this area to be honest.

Thank you again for your help, I apologise for the late response.

The BP can be added to the Tank’s BP and you can add it in to the alert function you have already and you can setup a branch just after tick that only goes true if the tank is in the alert state. Without the branch it will continuously update and follow the player. I used tick in the example because using a timer may not update the moment in a smooth fashion the way that tick will with each frame of gameplay.

@Black Phoenyx Wow that was a quick reply, thanks! The way the tank is set up it’s so that it starts moving “on pawn see” which would be it’s alert state that I could activate the “alert?” branch.

Then I have a trigger box parented to the turret in which if it detects the player, will fire a round.

Would using a timer actually work though? As I’m looking at what’s the most efficient too. Event triggers a timer or something, although I’m not sure. It’s because tick is generally quite a resource hogger.

Thanks for the reply!

@Black Phoenyx Is there a way to slow down the turret rotation so it’s not so pin-point accurate? Giving the player a chance to move out the way?

I really appreciate your help bud! Thanks so much.

Yep, make adjustments to the interp to speed, that is what controls the speed of the rotation.

It may be best to do some experiments to see which way will work best for your setup, try both ways and check the performance impacts. Using a timer may work but you may end with a situation where the player moves a large distance in relation to the rotation of the turret between timer updates, in such a case the turret’s rotation would snap from one direction instantly to another in order to face the player again.

@Black Phoenyx Dude, you’re a headache saver. I had done most of the BP nodes but just didn’t know about the Set Relative Rotation node. I think that’s the key to all of it.

Just wondering out of curiosity, I don’t suppose you know of a quick way to match the pitch and yaw of the tank against the terrain? It sort of appears like it’s floating, because it’s not matching the terrain angle, If that makes any sense.

Genuinely, thanks so much!

@Black Phoenyx Okay, I just tried the BP nodes that you showed and after trying it, I’m getting the same issue. The Tank turret points at the player, but as soon as “OnSeePawn” triggers and it starts moving towards the player using “AI move to”, The turret starts pointing in all sorts of directions, instead of at the player. Using the SetWorldRotation was even worse, as it caused the turret to just spin like a helicopter blade.

I removed the Alert branch from the Tick event and noticed that when the tank hasn’t triggered the “OnSeePawn” and AI Move To node, the turret points towards the player fine. It only seems to break when the tank is moving towards the player.

Do you have any ideas why this is still happening?

It took about 10 minutes to set up from memory, if you want to test it for yourself.

Edit: I realised I was using a Get Relative Rotation with the Set World Rotation node. After switching to Get World Rotation, It appears to be working now.

Good, glad you got it working.

@Black Phoenyx Do you have any ideas on how to fix the pitch and roll of the tank compared to the terrain? As it looks like it’s hovering above the surface.

Is the tank meant to be a hover tank? How is the tank mesh setup within the BP, how are the controls setup? Can you post a screenshot or a quick video showing your setup? With out seeing exactly how it is setup I can’t really point you in a definitive direction I can only point you in a few.

Direction 1: check your BP’s default use of the controller rotation axis.

Direction 2: If you are using a spring arm for your camera do the same thing, disabling or enabling the use controller rotation axis according to your specific needs.

Direction 3: Make a copy of your BP and try using the tank mesh as the BP’s root component so that all other sub components follow its rotation as a parent.

@Black Phoenyx It’s a basic AI Pawn, as an enemy, using a Static mesh for the chassis and a separate static mesh for the gun. It would probably look okay as a hover tank, but the final model will have tracks so the main chassis static mesh of the tank needs to move following the slopes and angles of the terrain. The top component is a capsule component that roughly covers the outside of the tank.Then the static mesh for the tank, followed by turret in the hierarchy.

I’m using Pawnsensing and Character Movement for the AI handling itself.

Did you ever figure out how to orient it to the terrain?? I am having this exact issue