Raycast hit detection not working for high velocity objects

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 :slight_smile:
Any help would be appreciated

And also please don’t tell me to use FastCast

Thank you so much!

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?

Here is the code that handles the physics

function Physics:Step(Deltatime,Gravity)
		self.Force += self.Mass * Gravity

		self.Velocity += self.Force / self.Mass * Deltatime
		self.Position += self.Velocity * Deltatime

		self.Force = Vector3.new(0,0,0)

	return self.Position
end

Here is the code that handles the collision

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.

But I want the collision function to return a hit once the projectile hits the object.

Never mind, I found a way to get it working!

how? tell us please, im also running into this problem

1 Like

mind sharing how? (sqorry for bumping i’m facing the same issue)

1 Like
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)

Also that Blacklist in raycast variables isnt that deprecated?