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.
asked May 02 '18 at 09:52 PM in Blueprint Scripting
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:
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: https://www.youtube.com/watch?v=uVbwhm5gHXo
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.
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_target*t) - p_turret)/p_projectile
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.
Follow this question
Once you sign in you will be able to subscribe for any updates here