Leading Projectiles

Godot Version

4.3

Question

Hey all, if the answer to this is “study vectors some more” that’s fair enough.
However, if anybody has a mind for these things and how to explain them I would gladly have your advice.

I’ve got a 3D game, where stationary turrets shoot at the player.
For that I’ve got a simple marker I point towards the player to which the meshes are attached

cannon_marker.look_at(player.global_position, Vector3(0, 1, 0), true)

Easy enough.

However, my player is on a PathFollow3D and is continuously pulled forward, which means the turrets are forever aiming at a point the player isn’t at anymore.

How would I lead these shots, so they fly towards where the player will be?
This is a level of vectors that is making my head spin.

Thanks in advance.

You need to know how far the player can travel over the course of time it takes for the bullet to reach them

var distance: float = cannon_marker.global_position.distance_to(player.global_position)
var bullet_travel_time: float = distance / BULLET_SPEED

var player_travel_distance: float = PLAYER_SPEED * bullet_travel_time
var lead_position: Vector3 = player.move_direction * player_travel_distance

cannon_marker.look_at(lead_position, Vector3.UP, true)

The trickiest part will be determining which direction the player is moving by a path3D. If you want perfect accuracy you will sample the path3D by the player’s current offset plus travel distance. If you want a realistic (and pretty good) accuracy you will store the player’s last position and calculate the direction from their last position.

2 Likes

Hey Gert, apologies for the late reply, but thank you for taking the time!

This code makes more sense than I thought it would, so thank you for that. I’ll need to mull over the direction part however.

I’ve tried to input a few different things into the “player.move_direction” and at best I have these turrets shooting where the player will be in ten minutes or ignoring the player’s position entirely.

I did make a mistake for a little while with the PLAYER_SPEED, which on this occasion would just be the incremental value I add to the Progress of the PathFollow.
I only use the player speed for sideways motion (imagine going down a tunnel, where going down the tunnel happens at a fixed rate.)
I think I still need a lot to do a bit of learning in regards to Vectors in 3D haha.

Figured out how to get the player’s direction following along the path3D.

Took a lot of faffing about with velocities before realising they were always wrong.

Thank you so much for the help, I wouldn’t have known where to begin otherwise.

var distance : float = cannon_marker.global_position.distance_to(player.global_position)
			var forward_direction = player.transform.basis.z.normalized()
			var player_fwd_speed = 5.0 # arbitrary testing value as this doesn't match the follow speed
			var bullet_travel_time : float = distance / 20.0 # replace by bullet speed
			
			# Inverted direction, based on the PathFollow heading South rather than North
			predicted_position = player.global_position + (-forward_direction * player_fwd_speed * bullet_travel_time)
			cannon_marker.look_at(predicted_position, Vector3.UP, true)
1 Like