Hello everyone,
I'm tring to code a function for my AI to shoot incoming projectiles. Here is what I got so far:
func can_hit(cible):
var a
var b
var c
var m_speed = missilespeedarray[missilelvl]
var c_velocity = cible.velocity
var cible_position = cible.position
a = m_speed*m_speed - c_velocity.dot(c_velocity)
b = -2*((cible_position-$Muzzle.position).dot(c_velocity))
c = -(cible_position-$Muzzle.position).dot(cible_position-$Muzzle.position)
var delta = (b*b)-(4*a*c)
var solution
var solution1
if delta == 0:
solution = -b/(2*a)
return solution
elif delta > 0:
solution = (-b + sqrt(delta))/2*a
solution1 = (-b - sqrt(delta))/2*a
return max(solution,solution1)
else:
return -1
I then check for the result "solution", and if it is positive I'm asking the IA to shoot the target with:
var dir = cible.position + can_hit(cible) * cible.velocity
rotation = dir.angle()
var b = Bullet.instance()
Unfortunately most of the time the IA shoot in a parallel fashion to the incoming projectile. I don't know why, but sometimes it also approximatively shoot the target.
I must miss something, but I don't understand what.