Ai bot that can shoot character while running?

Hi - we have a script we use that can make a turret shoot at a character. Problem is, it can’t hit a character that is running across it’s vision (laterally). It is always a stud or two behind the character. If the character stops running, or runs directly toward the turret, the turret can hit. We are using this code to print “l” the laser bolt (it’s a basepart)

			l.Size = Vector3.new(0.2,0.2,(tip.Position - target.Position).Magnitude+5)--creates laser size/length
			l.CFrame = tip.CFrame * CFrame.new(0,0,-((tip.Position - target.Position).Magnitude/2)-5)

even though it uses the target.Position at the very time it is drawing this part, it still misses a running chaharacter?

anyone?

1 Like

Utilize humanoid move direction to predict the player’s position.

2 Likes

man, this is a small treasure trove of stuff I haven’t learned to do yet. the code is really elegant too. Thanks, I’ll dig into this on Monday/Tuesday and if it works I will hit you the “solution” badge.

This is a really good video and youtube channel in general - really appreciate the reference.

OK, I was pretty sure this was the answer. But here’s the thing:

In the video, when the guy moves the target, his lobber still hits the target, even as it moves.

But if you make a HumanoidRootPart the target, and make the character run - it begins missing again.

However, I really appreciate the reference to that video. It had some interesting concepts I’m sure to use elsewhere.

Anyone know how to do this? How to make a shooting AI that can hit a running target?

So this video shows what occurs if I set the target to the characters HumanoidRootPart.

I have a print that tells what the projectile .Touched On the visible misses, it almost always shows an object it “hit”. (“UpperRightArm” or “HumanoidRootPart” or “LeftFoot”)
So it is somehow hitting these things - but visibly it doesn’t seem to be ?
This video shows what happens (cancollide is true on projectile) Sometimes it doesn’t even make visible contact with a character part, it just bounces off an invisible hitbox?
:

The video below is when “cancollide” is false:

while true do 
	target = nil
	task.wait(2)

	local position1 = workspace.From.Position

	for i,v in pairs (workspace:GetChildren()) do
		if v:FindFirstChild("Humanoid") then
			target = v:FindFirstChild("HumanoidRootPart")
		end
	end
	if target then
		local position2 = target.Position

		local direction = position2 - position1
		local duration = math.log(1.001 +direction.Magnitude * 0.01)
		position2 = target.Position + target.AssemblyLinearVelocity * duration
		direction = position2 - position1

		local force = direction / duration + Vector3.new(0, game.Workspace.Gravity * duration * 0.5, 0)

--
		local clone = game.ServerStorage.Projectile:Clone()
		clone.Position = position1
		clone.Parent = workspace	
		local touchedAlready = false
		clone.Touched:Connect(function(touchedPart)
			if touchedPart.Name ~= "From" then
				if touchedAlready == false then
					touchedAlready = true
					print(touchedPart)					
				end
			end
		end)
--		

		clone:ApplyImpulse(force * clone.AssemblyMass)
		clone:SetNetworkOwner(nil)
	end

end

The code is from the youtube you linked to, Suphi Kaner (really worth watching too - thanks!). I adjusted it to fire at the character’s humanoidRootPart, instead of at an object in Workspace called “To”.

The “touched” part is also my addition, it prints what the block hits. When I check the server to see what it’s been hitting, it often states a character part - even when it appears to miss.

I think you copied the script wrong. You put Position2 after instead of before.

Script:

while true do
	task.wait(3)
	
	local target = nil
	
	local position1 = workspace.From.Position

	for i,v in pairs (workspace:GetChildren()) do
		if v:FindFirstChild("Humanoid") then
			target = v
		end
	end
	
	if target then
		local rootPart = target:WaitForChild("HumanoidRootPart")
		local humanoid = target:WaitForChild("Humanoid")
		
		local position2 = rootPart.Position
		position2 = rootPart.Position + rootPart.AssemblyLinearVelocity

		local direction = position2 - position1
		local duration = math.log(1.001 + direction.Magnitude * 0.01)

		local force = direction / duration + Vector3.new(0, workspace.Gravity * duration * 0.5, 0)

		local clone = game.ServerStorage.Projectile:Clone()
		clone.Position = position1
		clone.Anchored = false
		clone.Parent = workspace
		
		local debounce = false
		
		clone.Touched:Connect(function(hit)
			if hit.Name ~= "From" then
				if not debounce then
					debounce = true
				end
			end
		end)

		clone:ApplyImpulse(force * clone.AssemblyMass)
		clone:SetNetworkOwner(nil)
	end
end
2 Likes

Not sure what you mean. Here is Suphi Kaner’s original code from the youtube you linked above. He does it there as well, I think?

while true do 
task.wait(2)

local position1 = workspace.From.Position
local position2 = workspace.To.Position

local direction = position2 - position1
local duration = math.log(1.001 +direction.Magnitude * 0.01)
position2 = workspace.To.Position + workspace.To.AssemblyLinearVelocity * duration
direction = position2 - position1

local force = direction / duration + Vector3.new(0, game.Workspace.Gravity * duration * 0.5, 0)

print (duration)

local clone = game.ServerStorage.Projectile:Clone()
clone.Position = position1
clone.Parent = workspace
clone:ApplyImpulse(force * clone.AssemblyMass)
clone:SetNetworkOwner(nil)


end

or am I getting this wrong?

Well if you look your changing position 2 after which is pointless. So putting it before is what you want. Try the code I gave, it worked for me.

but that’s suphi kaner’s own code?

Yeah I don’t know just try putting it before, it works. Could be because something else?

Use a laser and make the turret a bullet. This will never work with physics because the player on their client is always ahead of their actual position on the server. Make a direct raycast to its targets. For effects, use a beam, and make the beam attach from an attachment on the turret, to an attachment on the player’s character. Beams update to the exact position of the player. Turning FaceCamera on will make it look almost identical to a cylinder. If you want a cannon ball, make it explode with an area of effect that would affect the character.

1 Like

I have to admit its working a little better…But now it typically fires in front of the character, and still misses.

However, it works well enough for what we are doing, and I like it more than the result we were getting before.

A strange thing I noticed, when Suphi replots position 2,he does it because he wants to replot after calculating “duration” with the logarithmic. He needs the first “position2” so he can plot the duration, then he includes " * duration" in the replot of position2…so that the linearvelocity is multiplied by the length of time the bullet will travel.

I notice in your version you remove this. But when it comes down to it, it seems to work better without this line. ?

Weird.

I think I follow this. But I’m not sure what you mean by “make the turret a bullet”.

But you are saying use raycast to see if a hit is possible, and to deal the damage.

Use the beam effect, stretched between two attachment points, to give the visual?

Yes that is what I mean.

character limit

1 Like

If you want you can send a file. I tested it and it works perfectly unless you trick it by moving at the last second

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.