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

How would I make a part following the mouse snap to 1 stud?

Asked by 8 years ago

I have this script right here that makes a part follow the mouse (not the full script)

render:connect(function()
   if moving then
       currentPart.CFrame = CFrame.new(mouse.Hit.p)
   end
end)

But the problem is the part:

  1. Doesn't nap to the nearest whole stud
  2. It keeps moving closer to the camera, like floating in the air. I want it to stay snapped to the Y.

This is for a building system in a new game I'm making. Thanks!

0
Not sure if this will work but try this. CFrame.new(mouse.Hit.p.X, PosY, mouse.Hit.p.Z) PosY would be where you want the block on the Y axis. AZDev 590 — 8y

1 answer

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

You can prevent the floating effect by ignoring the part, by setting mouse.TargetFilter = currentPart (and resetting this to nil after the part is placed). To snap to the grid, you can do currentPart.CFrame = CFrame.new(math.floor(mouse.Hit.p.x+0.5), mouse.Hit.p.y, math.floor(mouse.Hit.p.z+0.5)) to round the x and y coordinates to the nearest whole number. If you want a different gridsize, do math.floor(mouse.Hit.p.x/gridSize+0.5)*gridSize for the x and z.

Ad

Answer this question