I wrote a custom collision detection method for projectiles in my game because .touched fires after the object collides and thats to late. Soooo, I thought I should check before the collision actually happens. The code below does this but it doesnt always detect it fast enough so the projectile flies through the object or bounces off.

I know I could just use raycast and have some sorta of incremental raycasting to adjust for bullet drop and stuff but that would make the tracer jitter.

Any help with this would be appreciated.

Code below is obviously not the whole code but it will help you get an understanding.

	CurrentThreadId = CurrentThreadId + 1
	local inUseThreadId = CurrentThreadId	
	local startPosition = bullet.Position
	
	RunService:BindToRenderStep("BulletPhysics"..CurrentThreadId,Enum.RenderPriority.Camera.Value-1,function()	
		-- QuickLog("THREAD_ID :: "..inUseThreadId)
		local Distance = (startPosition - bullet.Position).magnitude
		if Distance >= config.Physics.MaxDistance then
			QuickLog("BulletPhysics :: END : MaxDistance met.")
			RunService:UnbindFromRenderStep("BulletPhysics"..inUseThreadId)
		else
			local ray = Ray.new(bullet.CFrame.p, (bullet.CFrame.p+(bullet.CFrame.lookVector*2) - bullet.CFrame.p).unit * 300)
			local part, position = workspace:FindPartOnRay(ray, bullet, false, true)	
			
			if part and (bullet.Position - position).magnitude < 5 then
				QuickLog("BulletPhysics :: END : Collision detected.")
				bullet.Anchored = true
				RunService:UnbindFromRenderStep("BulletPhysics"..inUseThreadId)
			end				
			
		end
		
	end)

1 comment

0

It might be CFraming through it, use :GetTouchingParts