I have recently made a Building system but for some reason it flys towards the camera. I have spent hours trying to fix this error with no success.
https://gyazo.com/09df3bb688d3a81e47edbe75880a044e
Here is my code:
local mouse = game.Players.LocalPlayer:GetMouse() local Model = workspace:WaitForChild('Window1') local posX = mouse.Hit.X local posY = mouse.Hit.Y local posZ = mouse.Hit.Z local function Snap() posX = math.floor(mouse.Hit.X) posY = mouse.Hit.Y posZ = math.floor(mouse.Hit.Z) end mouse.Move:Connect(function() Snap() Model:SetPrimaryPartCFrame(CFrame.new(posX, posY + 3.7, posZ)) end) local function Place() local WindowCopy = game:GetService('ReplicatedStorage').Objects.Window1:Clone() WindowCopy.Parent = workspace WindowCopy:SetPrimaryPartCFrame(CFrame.new(posX, posY + 3.7, posZ)) end mouse.Button1Down:Connect(function() Place() end)
Thanks
The problem is that the ray from the mouse is hitting the object you're trying to build, thus moving it slightly closer to the camera each tick.
To bypass this you can use Mouse.TargetFilter, which determines what objects should be ignored when casting the ray.
Here's the syntax and context of the code:
Mouse.TargetFilter = workspace.Baseplate -- now the mouse ray will ignore the baseplate object
And here's your script but with the implementation:
local mouse = game.Players.LocalPlayer:GetMouse() local Model = workspace:WaitForChild('Window1') local posX = mouse.Hit.X local posY = mouse.Hit.Y local posZ = mouse.Hit.Z local function Snap() posX = math.floor(mouse.Hit.X) posY = mouse.Hit.Y posZ = math.floor(mouse.Hit.Z) end mouse.Move:Connect(function() Snap() Model:SetPrimaryPartCFrame(CFrame.new(posX, posY + 3.7, posZ)) end) local function Place() local WindowCopy = game:GetService('ReplicatedStorage').Objects.Window1:Clone() WindowCopy.Parent = workspace WindowCopy:SetPrimaryPartCFrame(CFrame.new(posX, posY + 3.7, posZ)) mouse.TargetFilter = WindowCopy -- Now the mouse's raycast will ignore the window end mouse.Button1Down:Connect(function() Place() end)