Help with bullets (NPC shooter, the bullets rarely hit the player)

Hi, so I have made an AI that shoots people. As you can see in the video, everything works, but I have one problem. The bullets only hit when I am standing still or facing the AI. If I am walking or jumping, the bullet wont hit me, how can I fix this?

(Ignore the animations btw, they are WIP, and I dont have a gun model yet)

Here is the script:

	
		local bullet = Instance.new("Part",workspace)
		bullet.Size = Vector3.new(0.2,0.2,0.3)
		bullet.BrickColor = BrickColor.new("White")
		bullet.Material = Enum.Material.Neon
		bullet.CFrame = Bot:FindFirstChild('Right Arm').CFrame
		bullet.CanCollide = false
		bullet.Touched:Connect(function(obj)
			if not obj:IsDescendantOf(script.Parent) then 
				local human = obj.Parent:FindFirstChild("Humanoid")
				if human then
					if obj.Name == "Head" then
						human:TakeDamage(100)
					else
						human:TakeDamage(math.random(30,40))
					end
				end
				bullet:Destroy()
			end
		end)
		
		bullet.CFrame = CFrame.new(Bot:FindFirstChild('Right Arm').Position, target.Position)
		bullet.Velocity = bullet.CFrame.lookVector * 700
		```

If you have any suggestions, let me know. Thank you

![Untitled video - Made with Clipchamp (9)|video](upload://fViFP5eG6wSdiGnUNMvWEpv4oBH.mp4)


It didnt upload the video for some reason. But here it is:

I think it’s because your movement is always changing, so the bot is always delayed when it decides to shoot the bullet. Basically by the time the bot finds your position and shoots, your already moved up. can you show your code where you detect the players position.

I get the player’s position here:

bullet.CFrame = CFrame.new(Bot:FindFirstChild('Right Arm').Position, target.Position)
bullet.Velocity = bullet.CFrame.lookVector * 700

(target is the player’s torso)

Yeah I saw that, but where do you get that target value, you might have something that delays your code that doesn’t allow your target value to always stay upto date so the bullet can hit the players current position

the target value is just the character’s torso, it doesnt have to update.

did you want the bot to hit it target every time it fires?

You should use raycasts for this. A moving object that’s being used to detect bullet collision is not a good method for a gun

Well, I’d like it to hit the target most of the time.

Thanks, I will check it out.
`

Yeah, raycasts work perfectly. Thank you

1 Like

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