I am making a build tool for my game and i want to know how to calculate the offset, I thought the easiest way to do it was to detect intersecting hitboxes,create a ray, then move in the opposite direction based on its magnitude. So i want to detect when this happens:
local pos = --raycastResult.Position
local norm = -- raycastResult.Normal
local item = -- block or item or whatever you are trying to place
local endPos = pos + (norm * (item.PrimaryPart.Size*.5))
item:SetPrimaryPartCFrame(CFrame.new(endPos))
oof then I’m not sure what you are trying to do, I thought you wanted the placing part offset from the part it was touching so that this wouldnt happen
I found the method I showed from a post by StickMasterLuke
with an example file DragModelExample.rbxl (23.4 KB)
while true do
Buildmode = Player:GetAttribute("Buildmode")
if Buildmode == true then
block = game.ReplicatedStorage.SelectBox:Clone()
block.Parent = workspace.Ignore
Mouse.TargetFilter = workspace.Ignore
raycastparams = RaycastParams.new()
raycastparams.FilterType = Enum.RaycastFilterType.Blacklist
raycastparams.FilterDescendantsInstances = {Player.Character}
elseif workspace.Ignore:FindFirstChild("SelectBox") then
workspace.Ignore.SelectBox:Destroy()
end
while Buildmode == true do
ray = workspace:Raycast(Player.Character.Head.Position ,Mouse.Hit.Position , raycastparams )
if ray ~= nil then
local pos = ray.Position
local norm = ray.Normal
local item = block
local endPos = pos + (norm * (item.Size*.5))
print("script run once")
item.CFrame = CFrame.new(endPos)
end
game:GetService("RunService").RenderStepped:Wait()
end
wait(0.5)
end
I did not think about using that xd wouldn’t it lag the game a lot?
ok it still runs super slow like once every minute.
game:GetService("RunService").RenderStepped:Connect(function()
-- render stepped fires before the next frame is rendered
-- cast a ray
-- position the block based on the raycast result
end)
p.s. that code is formatted poorly it’s hard to see what is going on