x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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!

Product Version: UE 4.12
Tags:
more ▼

asked Mar 08 '17 at 09:02 AM in Blueprint Scripting

avatar image

MikeyBGames
41 18 24 28

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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

alt text

Or using just world rotation like this:

alt text

more ▼

answered Mar 08 '17 at 01:34 PM

avatar image

Black Phoenyx
4.2k 165 9 170

avatar image MikeyBGames May 01 '17 at 10:13 PM

@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.

avatar image Black Phoenyx May 01 '17 at 10:28 PM

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.

avatar image MikeyBGames May 01 '17 at 10:49 PM

@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!

avatar image Black Phoenyx May 01 '17 at 11:20 PM

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.

avatar image MikeyBGames May 01 '17 at 11:26 PM

@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.

avatar image Black Phoenyx May 01 '17 at 11:33 PM

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

avatar image MikeyBGames May 02 '17 at 01:16 AM

@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!

avatar image MikeyBGames May 02 '17 at 03:13 AM

@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.

avatar image Black Phoenyx May 02 '17 at 10:55 AM

Good, glad you got it working.

avatar image MikeyBGames May 05 '17 at 04:11 AM

@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.

avatar image Black Phoenyx May 05 '17 at 01:00 PM

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.

avatar image MikeyBGames May 11 '17 at 02:43 AM

@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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question