Hi,
So right now I’m trying to improve my projectile system to be accurate using raycast’s, however it still has its issues, where when firing at a specific area, and the object obscures the path, it will go through the object, and go to the destination when it was fired from.
Visual Example from ms paint
Issue neing Ray continues as normal, and Projectile will pass through it
While this wouldn’t happen under most circumstances when using weapons like Pistols, Swords, or Shotguns, it becomes a big issue when using long range weapons, such as: Snipers, and Rifles, and I want to figure out a way to fix it for said weapons.
This is my code used for this system
(Its a big mess of OOP and not OOP, which I hope to improve later on)
self.FireEvent = Fire.OnServerEvent:Connect(function(Player, Position: Vector3)
self.RaycastParams.FilterDescendantsInstances = {Player.Character}; -- Filter out Player
local Root = Player.Character.PrimaryPart; -- Ray Origin
local Distance = Player:DistanceFromCharacter(Position); -- Distance from point fired at
local TimeDelay = Distance/self.ProjectileSpeed; -- Time it will take to get there
if self.RequiresAmmo then -- If the weapon uses ammo
self.Config:SetAttribute("Ammo",math.clamp(self.Config:GetAttribute("Ammo") - 1, 0, self.Config:GetAttribute("MaxAmmo")));
end
local DelayedPositions = {}; -- gets positions and directions for raycasting
for i = 1, self.ProjectileCount do -- gets total for all projectiles
local x = self.Random:NextNumber(-self.ProjectileSpread, self.ProjectileSpread) -- random angle if added
local y = self.Random:NextNumber(-self.ProjectileSpread, self.ProjectileSpread)
local Direction = CFrame.new(Root.Position, Position) * CFrame.Angles(math.rad(x), math.rad(y), 0) -- direction
local ExtDirection = Direction.LookVector * (self.Range - utl.BulletOffset) --Direction to fire from
-- BulletOffset is so the weapon can properly hit any targets in the area
local Result = workspace:Raycast(Root.Position, ExtDirection, Params); -- Raycast
DelayedPositions[i] = {Root.Position, ExtDirection, Direction.LookVector, Result and Result.Position+Result.Normal or Root.Position+ExtDirection}; -- Add Data to table
end
utl.GlobalEffectEvent:FireAllClients(self.Tool, DelayedPositions, self.Projectile, TimeDelay, self.Sounds)
print(`Ping...`);
task.delay(TimeDelay, function() -- Bullet Delay
warn(`PONG! ({TimeDelay})`);
for i = 1, #DelayedPositions do -- get data for raycasts
local Items = DelayedPositions[i];
if (Items[1] - Items[4]).Magnitude > self.Range then continue; end -- if the data exceeds the range, cotinue
local Result = workspace:Raycast(Items[4], Items[3] * utl.BulletOffset, Params) -- Cast ray with new info
if Result then -- Player checking stuff.
local Humanoid = self:VerifyTarget(Result.Instance)
if Humanoid then
local Damage = self.Damage.X * (self:IsHeadshot(Result.Instance) and self.Damage.Y or 1)
self:DealDamage(Humanoid, Damage);
end
end
end
end)
end)
How can I improve this code to work with weapons like Snipers, and Rifles?
Edit: I’m still looking to get this fixed as I’m not really sure where I should start wit this system.