Placing items causing weird glitches

local wallClone = wallModel:Clone()
wallClone.Parent = playersPlot.Camera
	
runService.RenderStepped:Connect(function()
	wallClone.PrimaryPart.Position = mouse.Hit.Position
end)

Thought this would be easy :man_shrugging: basically just trying to create basic placement like any other game. Move the object around, object follows the mouse. So far I have this with the intention that it should follow the players mouse around. But does the below glitch. Technically follows the mouse but, rapidly moves out, then back in, then back out, etc. Also the model ain’t moving. I thought changing the PrimaryParts position would affect everything inside the model?
com-video-to-gif%20(2)

Try setting mouse.TargetFilter to the CurrentCamera, what’s happening here is the mouse is hitting the object that you attached to the mouse position and getting stuck in weird loops.

To move the model, try Model:SetPrimaryPartCFrame() too! It moves all the parts relative to the primary part, and not just the one part :slight_smile:

PLEASE READ EDIT BELOW! :smiley:
Having done those 2 things, it doesnt do the weird glitch, but the wall now is like titled? and it rotates around as your move the mouse around. Wall should be straight up vertical, and it’s being cloned from a model that is vertical


EDIT

Managed to find a fix for it, using some reference from other placement systems:

function RoundVector(vector, unit)
return vector - Vector3.new(vector.X%unit, 0, vector.Z%unit)
end

function wall:StartPlacing(playersPlot, mouse)
	
	local wallClone = wallModel:Clone()
	wallClone.Parent = playersPlot.Camera
		
	runService.RenderStepped:Connect(function()
		wallClone:SetPrimaryPartCFrame(CFrame.new(RoundVector(mouse.Hit.p,1)+Vector3.new(0,(wallClone.PrimaryPart.Size.Y/2),0)))
	end)
end

More problems then arise though :confused: (constant problems :sweat_smile:)

  • can be moved anywhere in the game (I’m not 100% sure how to go about locking it to the baseplate grid)
  • if you move your mouse up the side walls, the wall goes up with it. Is there anyway to make it so the parts bottom basically is ‘stuck’ in a sense to the floor.

Example of the wall moving up the wall

Few ‘solutions’ that I think would have something to do with the above problems:

  • Getting the floors size / 2 to lock to part to only be placed within the baseplate.
  • Doing something like wallClone.PrrimaryPart.Size.Y / 2 could get the bottom of the PrimaryPart, and thus set it somehow so it’s Y value can’t go any higher (or lower) and thus keep it on the ground at all times

Any advice or help is greatly appreciated :slight_smile:

Also, another problem I have seen :sweat_smile:

The baseplate for placement has the position and size at round numbers, and the below image shows the hitbox locking to the same round numbers (so no decimals) but the hitbox ain’t in line with the studs, even tho they both have no decimals in their positions.

Is there a way to get it to be placing based on the base plate and not the Roblox world?? Because if I move the baseplate to a random position with decimals, it’d mess with the system, but if the parts lock to the grid of the base (which the size of it will always be round even numbers) then I wouldn’t have to stress about keeping the base to always be in a certain position to work

when you have your base not on an Int number but on a decimal the RoundVector won’t see that. and just return a number, what I do for my upcoming game called Blox Building is instead of using mouse.Hit.p is using an viewporttoray function.

function MouseTargetWithWhiteList()
	local IgnoreTable = {Base.PrimaryPart}
	for i,v in pairs(Base.PlacedThings:GetChildren()) do
		table.insert(IgnoreTable,v.PrimaryPart)
	end
	local unitRay = Camera:ViewportPointToRay(Mouse.X, Mouse.Y, 0)
	local ray = Ray.new(unitRay.Origin, unitRay.Direction * 1000)
	local MouseTarget,MouseHit,SurfaceNormal = workspace:FindPartOnRayWithWhitelist(ray,IgnoreTable)
	return MouseTarget,MouseHit,SurfaceNormal
end

What i am doing is Make only an white list, sush as the walls and floors it can be placed on.
then all you gotta now is a pointToObjectSpace from the target. and then just doing an modf to the right grid size.

hope this helps you. if you wish for some more source code send me an dm.

1 Like

Not really how I’d implement this into my code though? I can see what you mean about the RoundVector not actually detecting the base plate, but surely there’s a way for it to take into account a baseplate? Considering the vector is the mouse.Hit.p, surely putting the base into that would fix it?

You could have RoundVector check if the X and Z axis are beyond the baseplate’s X/Z size and then if so snap to the max size. You could also set the Y axis to wallClone.PrimaryPart.Size.Y/2 + Baseplate.Size.Y/2 to stop it from walking up the walls.

Also, using the method DutchDeveloper uses is very nice because you can bind that function to the UserInputService.InputChanged event like:

userInput.InputChanged:Connect(function(input, inGui)
	if input.UserInputType == Enum.UserInputType.MouseMovement and not inGui then
		MouseTargetWithWhiteList()

That way instead of recalculating every frame via RenderStepped, you are only calculating when the mouse moves.

4 Likes