Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Build System Flying Towards Camera?

Asked by 4 years ago

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

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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)
1
Thanks So much! It Worked smaug1678 10 — 4y
0
Make sure to accept answers that work. So that others know that it works as well as to reward the person who answered. Vinceberget 1420 — 4y
Ad

Answer this question