0
\$\begingroup\$

I'm having trouble trying to get a projectile thrown by a tower to hit the player quickly.

See the image below:

enter image description here

The projectile hits the ground very close to where the player was.

If the player did not move so much, the projectile would certainly hit him, but even by slowing the player down and increasing the speed of the shot, the projectile often does not hit the player.

What happens almost always is the projectile orbiting the player.

If the player stands still when the tower starts firing, the projectile hits him, the problem happens when the player moves, the projectile does not follow the player correctly.

Expected result:

enter image description here

I have already tried to modify the speed of the player and the projectile, but I have not obtained efficient improvements in the result. I tried to modify the gravity (of the projectile and its mesh), friction. All to no avail.

Projectile BP:

enter image description here


EDIT 1

With this code I was able to stop the projectile from crossing the floor:

enter image description here

But the projectile still orbits the player:

enter image description here

If the player does not stop moving, the projectile orbits it indefinitely.

Below is how the components went after I turned off the physics at all.

Sphere Collision:

enter image description here

Static Mesh:

enter image description here

ProjectileMovement:

enter image description here

\$\endgroup\$
8
  • \$\begingroup\$ You can check the “IsHommingPeojectile” checkbox in the ProjectileMovementComponent. \$\endgroup\$ – ColdSteel Sep 29 '19 at 1:18
  • \$\begingroup\$ Dont forget to disable all the physics in projectile and its components. \$\endgroup\$ – ColdSteel Sep 29 '19 at 1:18
  • \$\begingroup\$ Should I disable? Apply Impulse on Damage and Replicate Physics to Autonomus Proxy. \$\endgroup\$ – Boneco Sinforoso Sep 29 '19 at 17:20
  • \$\begingroup\$ Thats your game decisions. It is important to uncheck simulates physics on every component that are in the projectile actor, otherwise bad things will happen. \$\endgroup\$ – ColdSteel Sep 30 '19 at 12:30
  • 1
    \$\begingroup\$ Try to set homing acceleration magnitude to a gigantic number. \$\endgroup\$ – ColdSteel Oct 1 '19 at 11:56
1
\$\begingroup\$

Intercept the target

I'm not too familiar with UE, but the behaviour you describe is consistent with the projectile "naively" accelerating towards the target's current position (from your blueprint, it looks like a constant acceleration is indeed being applied).

To actually hit a moving target, you should predict its location at the time the projectile actually reaches it, and move towards that point*.

If the projectile is significantly faster than the target, an easy approximation for that point would be:

travel time = distance to target / projectile speed
point to move towards = target position + (target velocity * travel time)

(* Recalculate periodically, e.g. once per physics tick)

\$\endgroup\$
7
  • \$\begingroup\$ The OP wants a homing projectile - means if the target changed its direction/location/speed/acceleration it should still hit it. \$\endgroup\$ – ColdSteel Oct 1 '19 at 11:40
  • \$\begingroup\$ @ColdSteel And why do you think it wouldn't do that? \$\endgroup\$ – Ruther Rendommeleigh Oct 1 '19 at 11:43
  • \$\begingroup\$ Which would be just as easy as: ProjectileLocation = Normalize( targetLocation - ProjectileLocation) * speed * DeltaTime \$\endgroup\$ – ColdSteel Oct 1 '19 at 11:44
  • \$\begingroup\$ @ColdSteel Yes, I think that would work as well. It would take a slightly longer path and set object positions directly, which might have frame rate implications. \$\endgroup\$ – Ruther Rendommeleigh Oct 1 '19 at 11:49
  • 1
    \$\begingroup\$ @ColdSteel True. As I wrote, it's an approximation I'd recommend for a relatively fast projectile / slow target. \$\endgroup\$ – Ruther Rendommeleigh Oct 1 '19 at 11:57
1
\$\begingroup\$

This is probably what you need. It's for unity in C# but the maths all there.

http://wiki.unity3d.com/index.php/Trajectory_Simulation

I use this all the time and it is VERY accurate even at high speeds. It can't predict changes in direction like weaving but it will hit where the target is going to be on the dot every single time. If you want it to predict direction changes you need homing code added but you don't sound like you want homing missiles. You want projectiles.

Hope this helps.

\$\endgroup\$
2
  • \$\begingroup\$ Very good, but too complex for me yet. \$\endgroup\$ – Boneco Sinforoso Oct 9 '19 at 0:30
  • \$\begingroup\$ Ahhh, I'm telling you this is the magic. Study the crap out of it. I believe in you. This isn't very hard to implement. The hard part is understanding what to assign to what. You have all the pieces, you just need to point them all to the right place... I know how it is though. I wish I could have helped you more. \$\endgroup\$ – Daniel Awesome Oct 13 '19 at 4:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.