I made a system that allows me to pickup and hold objects.
when I am holding the object, it clips through walls essentially making it useless for the type of game I am making.
Every solution I have tried finding does not work.
local function HoldObject()
if not HoldingObject and CheckForHoldable() then
local Object = GetHoldable()
HoldingObject = true
CurrentHolding = Object
end
end
local function ReleaseObject()
if HoldingObject and CurrentHolding ~= nil then
CurrentHolding.CanCollide = true
CurrentHolding.Anchored = false
CurrentHolding = nil
HoldingObject = false
end
end
-- this function below is ran in RenderStepped
local function Update()
if HoldingObject and CurrentHolding ~= nil then
CurrentHolding.CFrame = CurrentHolding.CFrame:Lerp(Camera.CFrame * CFrame.new(0, 0, -4.5), 0.1)
CurrentHolding.CanCollide = false
CurrentHolding.Anchored = true
end
end
local function HoldObject()
if not HoldingObject and CheckForHoldable() then
local Object = GetHoldable()
HoldingObject = true
CurrentHolding = Object
end
end
You’re running the update function with rendersteeped, where you’re also setting cancollide to false
local function Update()
if HoldingObject and CurrentHolding ~= nil then
CurrentHolding.CFrame = CurrentHolding.CFrame:Lerp(Camera.CFrame * CFrame.new(0, 0, -4.5), 0.1)
CurrentHolding.CanCollide = false -- See this? It's false! It should be true!
CurrentHolding.Anchored = true
end
end
Cause you’re setting it to false every frame, it goes through walls. Set it to true and it should work
--//
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectionService = game:GetService("CollectionService")
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local PlayerService = game:GetService("Players")
local RunService = game:GetService("RunService")
--//
local Player = PlayerService.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local PlayerGui = Player:WaitForChild("PlayerGui")
local Camera = workspace.CurrentCamera
local Mouse = Player:GetMouse()
--//
local AssetsFolder = ReplicatedStorage:WaitForChild("assets")
local RemotesFolder = AssetsFolder:WaitForChild("remotes")
local ModulesFolder = AssetsFolder:WaitForChild("modules")
--//
local CheckLength = 8
local LastHoldable = nil
local CurrentHolding = nil
local HoldingObject = false
--//
local function CheckForHoldable()
local RayCast = Ray.new(Camera.CFrame.Position, Camera.CFrame.LookVector * CheckLength)
local Wall, HitPos = workspace:FindPartOnRay(RayCast, Character)
if Wall and CollectionService:HasTag(Wall, "Holdable") then
return true
else
return false
end
end
local function GetHoldable()
local RayCast = Ray.new(Camera.CFrame.Position, Camera.CFrame.LookVector * CheckLength)
local Wall, HitPos = workspace:FindPartOnRay(RayCast, Character)
if Wall and CollectionService:HasTag(Wall, "Holdable") then
return Wall
else
return false
end
end
--//
local function HoldObject()
if not HoldingObject and CheckForHoldable() then
local Object = GetHoldable()
HoldingObject = true
CurrentHolding = Object
CurrentHolding.CanCollide = true
end
end
local function ReleaseObject()
if HoldingObject and CurrentHolding ~= nil then
CurrentHolding.CanCollide = true
CurrentHolding.Anchored = false
CurrentHolding = nil
HoldingObject = false
end
end
local function Update()
if HoldingObject and CurrentHolding ~= nil then
CurrentHolding.CFrame = CurrentHolding.CFrame:Lerp(Camera.CFrame * CFrame.new(0, 0, -4.5), 0.1)
CurrentHolding.CanCollide = true
CurrentHolding.Anchored = true
end
end
--//
UserInputService.InputBegan:Connect(function(Key, Chat)
if Chat then return end
if Key.KeyCode == Enum.KeyCode.E then
HoldObject()
end
end)
UserInputService.InputEnded:Connect(function(Key, Chat)
if Chat then return end
if Key.KeyCode == Enum.KeyCode.E then
ReleaseObject()
end
end)
RunService.RenderStepped:Connect(Update)
I’m pretty sure this happens because you’re setting object CFrame to position your object, and that means your object changes position no matter what, physics don’t affect it in that case.
Your block is definitely colliding with the walls.
The block is trying to push away from the wall, but you have the block locked into a position relative to the player. This would explain why the block moves out of the wall when you release it.
It would also explain why players (and I assume other rigid bodies) are still colliding with the box. Rigid bodies are still able to push themselves away from the block while the walls cannot.
You kinda need to redo the grab system and do it different way. I’m not that experienced with making drag stuff myself so I recommend to find a dev forum post about it. The first one that catched my sight was this.