# How could I make this AI hit it's target more consistently?

Right now I have an AI hit and apply a velocity where the velocity is a .Unit from the AI’s torso and the player’s torso + their movedirection. It always overshoots it’s target or nails it, there is no in between.

What other system or math algorithm I could use to make the AI hit it’s target frequently?

1 Like

Would it help if I supplied any code that I’ve used in the math for the algorithm?

1 Like

Yeah it would be nice to know the the hit system is a hitscan ray for a projectile. I don’t believe it was mentioned clearly enough initially.

So it fires the function DoHit with the variable of the position the AI’s theoretical mouse would be.

``````dohit(enemywithpuck.HumanoidRootPart.Position+enemywithpuck.Humanoid.MoveDirection)
``````

then the function (which i can’t really post, it does a lot of if statements just to get to this main part)
calculates the velocity with this:

``````local bv = Instance.new('BodyVelocity')
bv.Name = 'HitVelocity'
bv.Parent = hrp
bv.P = 5000
bv.MaxForce = Vector3.new(math.huge,0,math.huge)
local function hitvel()
if power <= 25 then
bv.Velocity = (position-char.HumanoidRootPart.Position).Unit*(power*1.4)
else
bv.Velocity = (position-char.HumanoidRootPart.Position).Unit*(power*1.2)
end
end
hitvel()
``````

(power being on a scale of 0-50 where it’s calculated as distance from the player*1.5)

Hmm, I see this.

+enemywithpuck.Humanoid.MoveDirection

This indicates that the enemy’s mouse doesn’t account for the humanoid’s current velocity.

And I also realize the velocity function that you made isn’t constant and does an arbritary calculation of:

Overall, the targeting system you made doesn’t account for the physics required to hit a leading target. Seems like you will need to study target leading which will require lots of math. Here is a good website to start with:

1 Like

You can try taking the velocity of the player and adding that onto the players position and using that as the target point to shoot at

Multiply all of that value by this

I tried that but if you press A and then stop moving, the AI will entirely fly next to you because you velocity for that split second is Vector3.new(32,0,0) then it isn’t.

That wouldn’t work because the velocity changes in the span of miliseconds, like my response to @Complextic explains.

I was reading this but I’ve been having trouble calculating the angles due to the projectile(the player moving) is not static, it is sporadic and changes every millisecond where character Velocity is measured in Studs/Per Second. I couldn’t really find an quality way to apply this.

It might be a problem with Roblox humanoid being a pain to shoot and having fast acceleration and that the projectile speed is too slow to intercept. Moreover, by the time the projectile is fired the player can react just like you did and undo all the hard work the AI calculation has done:

Basically you need near hitscan levels of speed to hit default humanoid characters. So here are the possible solutions:

• Code a new movement system that makes humanoids acceleration in every slower

• Make bullet velocity very high

Then the AI would be able to hit more consistently.

Alternatively, you can use this neural network library to do machine learning and make the AI “Predict” where the player is going.

What I did now is took the velocity and multiplied it by a percentage based on calculating time it’d take for the AI to fly over to the target and hit him. It’s a pain but it’s whatever, your page explaining projectile interception helped wonders though, so thanks again for that!

2 Likes

Get the acceleration…

Velocity is speed magnitude with direction.

Acceleration is the change in velocity.

Get the acceleration and aim at that value.