How can I improve the aim of my turret?

I have made a basic turret that aims and shoots at the player (Blueprint attached). Because it aims at the player it usually misses because by the time it reaches its destination the player has moved.

While there are plenty of turret tutorials out there I have not found any that don’t have the same issue. I did find a blueprint which should do it but the image was too low-res to actually copy (Attached). It has annotations that describe what it does but I can’t recreate it myself without the nodes. It essentially uses the Bullet Velocity, player velocity and distance to calculate where the player will be.

If anyone can get this working it would be greatly appreciated.

The blueprint you attached should work fine besides a minor issue that I will describe after this. The comments describe exactly what the blueprint does, it is mostly mathematical equations formed with math nodes for floats and nodes such as “get actor location”.

As for the issue: Using this blueprint, after the calculated flight time, the projectile will have travelled the distance you calculated. This distance, however, is the distance between the turret and the target at the time the turret shot and not the actual distance between the turret and the target at the time the target is in the predicted location. In practice, this means that after the calculated flight time, the projectile is on the line between the turret and the predicted target position, but may be slightly too far or too close to the target.

To fix this, one has to solve for a position where the projectile location will be in the same position as the predicted target location at some time. Then, at a hit, the time that the projectile has travelled is equal to the time that the target has travelled. One finds the following equation:

t = |(p_target+v_target*t)-p_turret|/v_projectile

Here, ‘t’ is the time the projectile travelled, ‘p_target’ is the position of the target, ‘v_target’ is the velocity of the target, ‘p_turret’ is the position of the turret, and ‘v_projectile’ is the velocity of the projectile.

solving for ‘t’ yields

t = ((p_target + v_targett) - p_turret)/p_projectile
t
p_projectile = (p_target + v_targett) - p_turret
t
(p_projectile - v_target) = p_target - p_turret
t = (p_target - p_turret)/(v_projectile - v_target)

This time can then be used to calculate the predicted position of the target. This and pointing the turret in the right angle can be done as done in the attached blueprint.

I think I understand but I have no idea where to start when it come to transferring this to a blueprint.

(Btw, The screenshot with the annotation is something I found online. I can’t figure out how to use it though because it is zoomed out too far to read the actual nodes)

Something simpler you might want to consider for a “turret” since those usually aren’t “intelligent” per se is just setting the projectile speed to be significantly faster than the player movement speed. Everything posted above is true, you could do all those calculations but then you are creating a super smart turret that predicts player movement, maybe that’s what you want not sure if it is, then you have to do it with math, If not, changing the speed is a quick and easy fix.

I realise that would fix the issue but I actually want the player to be able to dodge the projectiles somewhat. At the moment any movement is enough to dodge which isn’t particularly engaging. Another soltion could be to get the turret to shoot every other shot a certain no. of degrees either side of the player. It’s simpler but might not work aswell.

So here is the problem I am seeing with your logic.

  1. Bullets always travel in a straight line. Therefore “dodging” the bullet (assuming it is relatively small compared to the player so you can’t “jump” or “duck” to avoid) will always be a result of changing direction off of that line. Any small movement therefore will cause the bullet to miss no matter how well placed it is. The reason bullets are hard to “dodge” isn’t because you have to move really far to get away from them it is because they move extremely fast compared to the player. So the time the player has to “move” off that straight line becomes the challenge.
  2. Having a super smart turret that can predict player movement and shoot to the location where the player WILL BE is great assuming the player remains moving in the same direction at the same speed. It is like a sniper shot, you calculate all this stuff including time to target and shoot where the target will be. But snipers will miss if those pre-calculated factors change once the bullet is in the air because the bullet can’t update itself in flight. So again it becomes an issue of speed/timing, how much time does the player have to change the direction or speed of movement
  3. What you might want to look into is a “homing” projectile. This is your basic heat seeking missile which will then “burn out” after a certain time period. This will make the player have to move great distances to “dodge” but usually the player is something with a great deal of speed to match the missile like an airplane so then it becomes an issue of out maneuvering the missile.
  4. Additionally you might want to look into a turret that “spams” a certain area, so it just shoots off multiple shots in various directions possibly with a staggered spread so the player will have to time when to move left then right then right, left, etc. This will work for relatively slower projectiles compared to player speed so that it now becomes an issue of reaction time to dodge

But again even #4 it still is an issue of getting off that “straight line” path and always will be with a bullet, only difference is keeping track of how many paths you need to avoid and when. Homing projectiles are the only thing that I know of that will make it hard to “dodge” without a great deal of movement.

If you are trying to hit a target with a single shot, having the turret shoot to where you expect the target to be gives you the most chance to hit. Therefor that is the way to go. He can always make it more difficult by adding turrets, faster bullets, more bullets etc., but that is not an answer to the question.

@gibospartan, I understand that the screenshot is something you found online. What I tried to explain to you in my answer is that the blueprint is no more then a realization of a mathematical equation. For example, you could realize the formula i found, “t = (p_target - p_turret)/(v_projectile - v_target)”, using “get” nodes to get the values of p_target, p_turret, v_projectile and v_target, and then dragging the outputs to nodes like “float - float” or “float/float”.

Wasn’t “answering” the question per se. I was just saying that the idea that “only a little bit of movement” causes the bullet to miss is always the case. If you run 5 mph forward and someone calculates where you are going to be 5 sec from now at that speed and shoots to have a bullet arrive at that exact position given flight time, it will still miss if the player say runs 5.1 mph if the bullet is going slow enough (the error on the calculation gets multiplied for every second of travel time) or the player is small enough (the window to hit is very narrow and small errors in calculation throw this off). Any change to the current vector of the player will cause a bullet to miss. So I am not understanding the basis of the question. In a gun fight between 2 human players there are constant calculations as to where you “expect” the other person to be. But once you fire the bullet if the other player deviates even a little bit from the expected path the bullet misses. The issue arises when the players don’t have the “time” to deviate due to bullet speed. It is always a question of speed. It is hard to dodge a bullet shot from point blank range because there is not enough reaction time. A sniper shot from 1 mile away however has a good chance of missing because there are literally seconds to change the direction you are going. So I respectfully disagree. “Where you expect the target to be” is only an issue because of the speed of the bullet. With a fast enough bullet “calculating” where the player will be ends up not factoring into the equation since the player could only move an infinitesimally small distance in the time it takes a bullet traveling at the speed of light for example to arrive at the player’s current location.

Also, I am not disagreeing with anything you’re telling the OP to do to create said mathematical calculations. I guess I just take issue with the basic premise of the question but that is neither here nor there.

gibospartan stated that he wants the projectiles to be dodged, but that now any movement is enough. This includes continuing in the same direction. (“by the time it reaches its destination the player has moved”).

It then remains true that a little change in movement can cause the projectile to miss. But I reckon that that will be sufficient, as the enemy’s reaction time and possibly also their inertia is against them. Besides that, I expect the time the enemy has to dodge the projectile to be relatively small still. I think that if you chose a small value, even just a second, the projectile can still be dodged.

It will always be true that a faster projectile, more projectiles or homing projectiles will be more accurate like you said, but these solutions will be equally if not more difficult to realize.

The speed of the projectile shouldn’t be hard, that’s just a setting in the projectile movement details panel. I guess I am just trying to visualize the game play mechanics and maybe that’s where my issue is trying to find something other than tick or speed to help. If you shoot a giant ball out of the turret with it set to the player’s current location then depending on the speed of the projectile relative to player speed/reaction time as they attempt to move out of the target line will determine if the ball hits. Increasing projectile speed increases the likelihood of hitting due to less reaction time. If the player is moving at a constant speed then a simple math calculation to direct the ball on the player’s vector movement line will create a 100% accuracy rating because it is all a mathematical formula. IF however, the player changes direction or speed and there is sufficient travel time for the projectile this will cause a miss due to the extrapolation of this error on the initial calculation for location of the hit point. So my question really is, what kind of game play mechanics are you trying to achieve? Because in a real gun fight hits occur due to a combination of bullet speed and player’s NOT changing direction quick enough. Think of something like the Matrix, bullets are dodged because in the matrix one can move so fast bullets can be seen traveling towards you and you can dodge them. Or characters like The Flash have the same ability, their relative speed is so great they can move around things like bullets. But for the average human bullet speed >> player speed. So pulling the trigger with a person standing 60 ft in front of you is a nearly a 100% hit. Throwing a baseball at someone the same distance and your chances of hitting are significantly less. Calculating the “expected” position still won’t change the fact that a small deviation from that path will cause a miss if player speed >> bullet I guess that is all I am trying to say

I tried to make a blueprint but found an error in my math, I hope to fix this later

Here is my answer to your question.

Enjoy!

I guess it is more accurate to think of my turret not like an actual turret with realistic behaviour. Instead it is more like one of the imps from the original Doom that shot fireballs (Except my enemy won’t move).

As I wrote this I looked them up and the imps actualy behave kind of like my basic enemy. That said my enemy still feels too easy.

There seems to be plenty of solutions and I’ll give each a test to see what works for me (Plus any people who see this thread in future could do with multiple solutions to find which suits them). So far I’ve got:

  1. Predicting the player location
  2. Faster Bullets
  3. Bullet Spread
  4. Homing Projectiles

The first is quite complicated, the formula I understand but getting it into a blueprint is complicated since certain nodes have multiple pins for each axis and even just knowing which nodes give the correct outputs for the calculation. I’m going to try but this is largely undocumented so I think it’s worth somone get a working blueprint if they can. If me or somone else gets a working version I’ll make a tutorial for other people so it can be replicated.

The second is easy. You just go into Projectile movement and change the speed and max speed.

I found a blueprint for the third: https://i0.wp.com/shootertutorial.com/wp-content/uploads/2015/08/fire1.jpg

I also found a tutorial for the last: UE4 Tutorial: MMO style homing spell projectile - YouTube

Like I say, I’m going to try the first option to get it working. If I do I’ll post the blueprint and mark the question as answered. If I fail I’ll probably post my blueprint here and hope somone can fix it.

Like I say I’ll make a tutorial if I get it working since there is no documentation I can find of how to do it. If I can’t get it done then hopefully somone else has the knowhow and 45 spare minutes to test it. Like I say there is no documentation so if you can get it working you’d be doing budding UE4 devs a service.