Im adding a building mechanic to my game currently
You open a menu, select the part, and place, simple!
Works fine with my meshpart sandbag
But when a part of any size or shape is used, it will float to the ground.
Video and code:
run.Stepped:Connect(function()
local mouseRay = mouse.UnitRay
local castRay = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000)
local ignoreList = {structure, plr.Character, workspace.Bullets, workspace.Shells, workspace.Mags}
local hit, position = workspace:FindPartOnRayWithIgnoreList(castRay, ignoreList)
local distance = (plr.Character.HumanoidRootPart.Position - structure.Position).Magnitude
if not hit or distance > maxPlacingDistance then
goodToPlace = false
structure.BrickColor = BrickColor.new("Crimson")
else
goodToPlace = true
structure.BrickColor = BrickColor.new("Forest green")
end
local cframeAngles = CFrame.Angles(0, math.rad(yOrientation), 0)
local cframe = CFrame.new(position.X, position.Y + yBuildingOffset, position.Z)
structure.CFrame = cframe * cframeAngles
end)
– my apologies for the intentation, this is only on the forum
video: robloxapp-20220825-1353393
What’s structure defined as?
04nv
(04nv)
3
Make sure it’s anchored, and it can’t be collided with, since this is a preview of the actual part I assume.
local Parameters = RaycastParams.new()
Parameters.FilterType = Enum.RaycastFilterType.Blacklist
Parameters.IgnoreWater = true
structure.Anchored = true
structure.CanCollide = false
game:GetService("RunService").Stepped:Connect(function(time, deltaTime)
local rootpart = select(2, pcall(function()
return plr.Character.HumanoidRootPart
end))
if typeof(rootpart) == 'Instance' then
local unitray = mouse.UnitRay
local raycastresult = workspace:Raycast(unitray.Origin, unitray.Direction * 1e3, Parameters)
if typeof(raycastresult) == 'raycastresult' then
local hit = raycastresult.Instance
local position = raycastresult.Position
local distance = (rootpart.Position - structure.Position).Magnitude
if typeof(hit) == 'Instance' and distance < maxPlacingDistance then
goodToPlace = true
structure.BrickColor = BrickColor.new("Crimson")
else
goodToPlace = true
structure.BrickColor = BrickColor.new("Forest green")
end
local cframeangles = CFrame.fromEulerAngles(0, math.rad(yOrientation), 0)
local cframe = CFrame.new(position.X, position.Y + yBuildingOffset, position.Z)
structure.CFrame = cframe * cframeangles
end
end
end)
It may help to use WorldRoot:Raycast