Help Needed: Adding Collision Response

Hello everyone,

I’m currently working on a project in Roblox Studio and need some help with adding collision response to my local script. I’ve created a bounding box that detects collisions, but I’m struggling to implement a proper response when a collision is detected. Any advice or guidance would be greatly appreciated!

Current Script:

Here’s the code I have so far:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local rootPart = character:WaitForChild("HumanoidRootPart")
local boundingBoxPart = Instance.new("Part")

local function createBoundingBox()
	local size = Vector3.new(2.5, 5, 2.5)
	local region = Region3.new(rootPart.Position - size / 2, rootPart.Position + size / 2)

	boundingBoxPart.Size = size
	boundingBoxPart.Anchored = true
	boundingBoxPart.CanCollide = false
	boundingBoxPart.Transparency = 0.5
	boundingBoxPart.Color = Color3.new(0, 1, 0)
	boundingBoxPart.CFrame = CFrame.new(rootPart.Position + Vector3.new(0, -0.5, 0))
	boundingBoxPart.Parent = workspace

	local ignoreList = {character, boundingBoxPart}

	local partsInRegion = workspace:FindPartsInRegion3WithIgnoreList(region, ignoreList, math.huge)
	for _, part in ipairs(partsInRegion) do
		if part and part.Parent ~= character then
			print("Bounding box hit: " .. part.Name)
			local rayOrigin = rootPart.Position
			local rayDirection = (part.Position - rayOrigin).unit * 10
			local raycastParams = RaycastParams.new()
			raycastParams.FilterDescendantsInstances = ignoreList
			raycastParams.FilterType = Enum.RaycastFilterType.Exclude

			local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
			if raycastResult then
				local hitNormal = raycastResult.Normal
				print("Hit normal: " .. tostring(hitNormal))
			end
		end
	end
end

game:GetService("RunService").RenderStepped:Connect(function()
	createBoundingBox()
end)

obviously the bounding box is for visualization

1 Like

I dont fully understand what you need help for.
You could use a touched event, maybe?

boundingBoxPart.Touched:Connect(function()
-- Put code here
end)

basically what i want to know is how to add a collision response to when the raycast detects something, the bounding box is just there for visualization(for debug/testing purposes), i couldn’t think of a approach to adding collision response like source engine’s(TF2, HL2, etc) character controller’s collision and hitbox

Do you mean like pushing the object, or detecting whether or not the object has been hit?

detecting whether or not the object has been hit, it does detect parts and everything, it just needs collision response to the rootpart