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