Hello! So, I’m trying to make an fps game with projectile based guns. Basically bullets with travel time and bullet drop n’ stuff. So to detect a collision I raycast from the projectile’s position, this works fine If the projectile isn’t traveling at a high speed, but it doesn’t detect the hit at high velocity. I found a solution by raycasting from the projectile’s previous position to it’s current position, but how would I go by implementing this to Roblox? Or if you have another solution, please tell me
Any help would be appreciated
You need the ray to go at least as far as the bullet will travel in that space of time. Personally when I do it my bullets are entirely raycasted and I use c frames to create the physics. Everything is scripted and not handled by Roblox.
I guess using property change signal position might work, check if previous position not a nil then create a raycast between the current position and the previous, after that change the previous position to the current position.
I made a custom physics module for the projectile movement, but what do you mean by “You need the ray to go as far as the bullet will travel in that space of time”?
There was a guy here recently who used Roblox physics but used raycasting for the hit detection, and the physics-based projectile moved too fast and passed through an object without the ray detecting anything. If you’re handling the physics yourself, that’s not the issue. Can I see the functions which handle physics and raycasting?
function Physics:Collide()
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
local currentpos = pos --Position of projectile
local direction = vel.Unit --The velocity of the projectile, problems is located here
local ray = workspace:Raycast(currentpos,direction,params)
if ray ~= nil then
print("Hit")
hit = true
end
end
Direction needs to be velocity, so ditch the .Unit. Otherwise the bullet will only have a chance of detecting one stud out of however many studs Velocity is. So if Velocity is 15 studs per frame, you’d only be able to detect a hit in the first stud, and then skip detection for the next 14 studs.
local RunService = game:GetService("RunService")
local part = script.Parent
local previousPosition = part.Position
RunService.Stepped:Connect(function()
local origin = part.Position
local direction = (origin - previousPosition) * (origin - previousPosition).Magnitude
local raycast = workspace:Raycast(origin, direction)
if raycast then
print("hit")
end
previousPosition = origin
end)