Hello there, developers of Roblox! Recently, I have made a Police officer AI that can shoot you if you are wanted. Sadly, the officer just shoots through walls, and I need to check if there are any obstacles in the GUN to CHARACTER direct path. I need to generate a part from two Vector3 positions. First would be the barrel of the gun, and second being the characters HumanoidRootPart
. I have no idea on how to make this, so I need some help. Thanks in advance, Edryi007.
this is an example of how to do it
local gun = script.Parent
local bulletSpeed = 100
local bulletRange = 200
local bulletDamage = 10
local muzzle = gun.Handle.Muzzle
local function fireGun()
local bulletDirection = muzzle.CFrame.LookVector
local raycastResult = workspace:Raycast(muzzle.Position, bulletDirection * bulletRange)
if raycastResult then
-- Get the part that was hit
local hitPart = raycastResult.Part
if hitPart and hitPart.Parent:FindFirstChild("Humanoid") then
hitPart.Parent.Humanoid:TakeDamage(bulletDamage)
end
end
local bullet = Instance.new("Part")
bullet.Shape = Enum.PartType.Ball
bullet.Size = Vector3.new(0.5, 0.5, 0.5)
bullet.Position = muzzle.Position
bullet.Velocity = bulletDirection * bulletSpeed
bullet.CanCollide = false
bullet.Parent = workspace
game:GetService("Debris"):AddItem(bullet, bulletRange / bulletSpeed)
end
gun.MouseButton1Click:Connect(fireGun)
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.