Im making a placement system for my game. It starts by raycasting 10 studs in front of your hrp to avoid going through walls, it then goes down 20 studs to clip to the ground, only problem is sometimes when you jump it moves the object up above you which shouldnt be happening. When it goes up on the wall it also turns green meaning its viable plcement.
local BUFFER = 1
tool.Equipped:Connect(function()
local tempModel : Model = powerupModels:FindFirstChild(powerupname):Clone()
tempModel.Parent = workspace.CurrentCamera
transformMaid:GiveTask(RunService.Heartbeat:Connect(function()
if not hrp or not tempModel then return end
--raycast in front display offset, then down 10 studs and place there
--front raycast
local baseY = hrp.Position.Y - 2
local startPointForward = hrp.Position + Vector3.new(0,baseY,0)
local directionForward = hrp.CFrame.LookVector * DISPLAY_OFFSET
local forwardHit = utilities.raycastModule(startPointForward, directionForward, {char})
local forwardPos
if forwardHit then --stop before wall
forwardPos = forwardHit.Position - hrp.CFrame.LookVector * BUFFER
else
forwardPos = hrp.Position + hrp.CFrame.LookVector * DISPLAY_OFFSET
end
local downOrigin = forwardPos + Vector3.new(0,DISPLAY_OFFSET,0)
local downDirection = Vector3.new(0,-20,0)
local GroundHit = utilities.raycastModule(downOrigin, downDirection, {char})
if GroundHit then --can place ere
tempModel:PivotTo(CFrame.new(GroundHit.Position))
tempModel.Wall.Color = Color3.new(0,1,0)
else --cant place
tempModel:PivotTo(CFrame.new(forwardPos))
tempModel.Wall.Color = Color3.new(1,0,0)
end
end))
end)```