Hello! I am currently working on a placement system using raycasts. While coding, I couldn’t wrap my head around one particular issue that I continue to face while using raycasts. Whenever the raycast was finished and I fetched the result using hit.Position
, hit.Position
would be found in the middle of whatever it was casted to (i.e BaseParts). I confirmed this by placing a Cylinder at the end of the raycast. The expected behavior is that the ray will be cast wherever the MousePosition is, using MouseRay.Origin
and MouseRay.Direction.
Any help on this matter is greatly appreciated!
for i,v in ipairs(game:GetService("ReplicatedStorage").Ghosts:GetChildren()) do
if v.Name == placementUI.Main.ObjName.Text then
print(v.Name)
local ghost = v:Clone()
ghost.Parent = workspace
--Raycast
local mouseRay = mouse.UnitRay
local raycastParams = RaycastParams.new()
raycastParams.IgnoreWater = true
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = filtered
local raycast = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 9999, raycastParams)
if raycast then
local hit = raycast.Instance
print(hit.Position)
local cylinder = Instance.new("Part")
cylinder.Shape = Enum.PartType.Cylinder
cylinder.Size = Vector3.new(1,50,1)
cylinder.Position = hit.Position
cylinder.Transparency = 0.6
cylinder.CanCollide = false
cylinder.Anchored = true
cylinder.Parent = workspace
table.insert(filtered, cylinder)
end
end
end
end
The position of the raycast in the middle of the BasePart
14:47:51.516 failed placement @ 06 Aug 2022 14:47 auto-recovery file was created - Studio
14:47:55.667 Cone - Client - LocalScript:34
14:47:55.667 -144.248291015625, 0.4961013197898865, -48.50074005126953 - Client - LocalScript:48