Godot Version
4.5.1.stable
Question
hi, i’m trying to make an enemy shoot where the player is going instead of where they are, however when i stay still the bullet dosen’t move at all and if i move the bullet shoots in the wrong direction.
this is the formula i’m using
func attackWithPrediction(target: Entity) -> void:
var bullet: Projectile = NEEDLE.instantiate()
var timeToHit: float
var predictedPosition: Vector2
bullet.global_position = global_position
timeToHit = global_position.distance_to(target.global_position) / bullet.speed
predictedPosition = target.velocity * timeToHit
bullet.direction = predictedPosition.normalized()
get_tree().root.add_child(bullet)
here is a video of the behavior
edit:
ok, the behavior has improved a lot now, it still isn’t perfect, but i prefer the enemies to have imperfect aim, at least now they are trying to hit you ![]()
i’ll leave the code here in case someone needs it
##predics where the target will be and attacks there
func attackWithPrediction(target: Entity) -> void:
var bullet: Projectile = NEEDLE.instantiate()
bullet.global_position = global_position
bullet.direction = interceptTarget(target.global_position,global_position,target.velocity,bullet.speed)
if not bullet.direction.is_zero_approx():
get_tree().root.add_child(bullet)
##uses data to intercept target and returns the predicted location
func interceptTarget(targetPosition: Vector2,shooterPosition: Vector2, targetVelocity: Vector2,projectileSpeed: float) -> Vector2:
var targetToShooter: Vector2 = shooterPosition - targetPosition
var DC: float = targetToShooter.length()
var alpha: float = targetPosition.angle_to(targetVelocity)
var SA: float = targetVelocity.length()
var r: float = SA / projectileSpeed
var positionToShoot: Vector2 = aimMath.solveQuadratic(1 - r * r,2 * r * DC * cos(alpha),-(DC * DC))
var directionToShoot: float = max(positionToShoot.x,positionToShoot.y)
var timeToHit: float = directionToShoot / projectileSpeed
var predictedTargetLocation: Vector2 = targetPosition + targetVelocity * timeToHit
return shooterPosition.direction_to(predictedTargetLocation)
class aimMath:
##retruns a vector containing (root1,root2)
static func solveQuadratic(a: float, b: float, c: float) -> Vector2:
var root1: float
var root2: float
var discriminant: float = b * b - 4 * a * c
if discriminant < 0:
return Vector2(0,0)
else:
root1 = (-b + sqrt(discriminant))/(2 * a)
root2 = (-b - sqrt(discriminant))/(2 * a)
return Vector2(root1,root2)