Well, I kinda want to understand how it works so I don’t have to ask this question multiple times, could you explain how it works? Thanks for taking your time to post
ok, so in loleris’ post, what he is essentially doing is firstly:
Creating a CFrame 100 studs forward from the gun’s barrel
Rotating it so that it is pointed sideways by a Random amount, (why he does math.pi * 2 * math.random(): he is just getting 360 degrees in radians, then multiplying that by a random number in between 0 and 1 to get a random amount of rotation between 0 and 360)
multiplying its cframe times a cframe that is a few random studs to the side, to make the bullet actually move from the exact cframe it was hit in TOWARDS the rotation it was just given.
instead of writing the entire script, the method i always use when doing shooting calculations is this:
function sphericalMaxDistPos(maxRange)
return camera.Focus.p + (mouse.Hit.p - camera.Focus.p).Unit * maxRange
end
local pos = mouse.Target and (mouse.Hit.p - camera.Focus.p).Magnitude <= maxGunRange and mouse.Hit.p or sphericalMaxDistPos(maxGunRange)
local rayparams = RaycastParams.new()
rayparams.FilterType = Enum.RaycastFilterType.Blacklist
rayparams.FilterDescendantsInstances = {localCharacter}
local ray = workspace:Raycast(camera.Focus.p, (camera.Focus.p - pos).Unit * maxGunRange, rayparams)
if ray and ray.Instance then
print("Part found!")
end
local maxGunRange = 100
local camera = workspace.CurrentCamera
local debris = game:GetService("Debris")
game.ReplicatedStorage.ShotgunFire.OnServerEvent:Connect(function(player, Character, HEADSHOT, BODY_DAMAGE, SHOOTY_PART, MousePos, HandleOfGun, Part_1, Part_2, Part_3, mouse)
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {Character, SHOOTY_PART, HandleOfGun, Part_1, Part_2, Part_3}
local function sphericalMaxDistPos(maxRange)
return camera.Focus.p + (mouse.Hit.p - camera.Focus.p).Unit * maxRange
end
local pos = mouse.Target and (mouse.Hit.p - camera.Focus.p).Magnitude <= maxGunRange and mouse.Hit.p or sphericalMaxDistPos(maxGunRange)
local ray = workspace:Raycast(camera.Focus.p, (camera.Focus.p - pos).Unit * maxGunRange, raycastParams)
if ray and ray.Instance then
print("Part found!")
end
local function RandomSpread(cf, spread_diameter)
local target_cf = cf * CFrame.new(0, 0, -100) -- Get a CFrame 100 studs ahead
* CFrame.Angles(0, 0, math.pi * 2 * math.random()) -- Turn CFrame randomly on the Z axis
* CFrame.new(0, math.random() * spread_diameter / 2, 0) -- Pick a random spread distance from the origin ray
return target_cf.p
end
local bulletpos = RandomSpread(SHOOTY_PART.CFrame, 10)
local direction = (bulletpos - SHOOTY_PART.Position).Unit * 100
local midPoint = SHOOTY_PART.Position + direction/2
local part = Instance.new("Part")
part.Size = Vector3.new(0.2, 0.2,direction.Magnitude)
part.CFrame = CFrame.new(midPoint, SHOOTY_PART.Position)
part.Anchored = true
part.CanCollide = false
part.Parent = workspace
part.BrickColor = BrickColor.new("New Yeller")
debris:AddItem(part,0.1)
if ray then
local part = ray.Instance
print(part)
local humanoid = part.Parent:FindFirstChild("Humanoid")
print("HumanoidFound")
if part.Name == "Head" then
humanoid.Health = humanoid.Health - math.random(20,40)
else
humanoid.Health = humanoid.Health - math.random(10,15)
end
end
end)
local maxGunRange = 100
local camera = workspace.CurrentCamera
local debris = game:GetService("Debris")
game.ReplicatedStorage.ShotgunFire.OnServerEvent:Connect(function(player, Character, HEADSHOT, BODY_DAMAGE, SHOOTY_PART, MousePos, HandleOfGun, Part_1, Part_2, Part_3, mouse)
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {Character, SHOOTY_PART, HandleOfGun, Part_1, Part_2, Part_3}
local function sphericalMaxDistPos(maxRange)
return camera.Focus.Position + (mouse.Hit.Position - camera.Focus.Position).Unit * maxRange
end
local pos = mouse.Target and (mouse.Hit.Position - camera.Focus.Position).Magnitude <= maxGunRange and mouse.Hit.Position or sphericalMaxDistPos(maxGunRange)
local ray = workspace:Raycast(camera.Focus.Position, (camera.Focus.Position - pos).Unit * maxGunRange, raycastParams)
local function RandomSpread(cf, spread_diameter)
local target_cf = cf * CFrame.new(0, 0, -100) -- Get a CFrame 100 studs ahead
* CFrame.Angles(0, 0, math.pi * 2 * math.random()) -- Turn CFrame randomly on the Z axis
* CFrame.new(0, math.random() * spread_diameter / 2, 0) -- Pick a random spread distance from the origin ray
return target_cf.Position
end
local bulletpos = RandomSpread(SHOOTY_PART.CFrame, 10)
local direction = (bulletpos - SHOOTY_PART.Position).Unit * 100
local midPoint = SHOOTY_PART.Position + direction/2
local part = Instance.new("Part")
part.Size = Vector3.new(0.2, 0.2,direction.Magnitude)
part.CFrame = CFrame.new(midPoint, SHOOTY_PART.Position)
part.Anchored = true
part.CanCollide = false
part.Parent = workspace
part.BrickColor = BrickColor.new("New Yeller")
debris:AddItem(part,0.1)
if ray then
local part = ray.Instance
print(part)
local humanoid = part.Parent:FindFirstChild("Humanoid")
print("HumanoidFound")
if part.Name == "Head" then
humanoid.Health = humanoid.Health - math.random(20,40)
else
humanoid.Health = humanoid.Health - math.random(10,15)
end
end
end)