How to make part "collide" when dragged by mouse

Making a part placement system, a semi transparent part goes to the mouse position (on client) for a preview, however, both the palced block and the preview goes underneath the ground due to its center being at the mouse position (which is exactly the surface of the ground)

I have searched a bit but I literally dont know what I would search for this

What it currently is:

where I want it to be:

Code for position:

if dum:IsA("BasePart") then
		place = run.RenderStepped:Connect(function()
			dum.CFrame = CFrame.new(mouse.Hit.Position.X, mouse.Hit.Position.Y, mouse.Hit.Position.Z) * CFrame.Angles(math.rad(rot["r"]), math.rad(rot["t"]), math.rad(rot["y"]))
		end)
	else
		place = run.RenderStepped:Connect(function()
			dum:PivotTo(CFrame.new(mouse.Hit.Position.X, mouse.Hit.Position.Y, mouse.Hit.Position.Z) * CFrame.Angles(math.rad(rot["r"]), math.rad(rot["t"]), math.rad(rot["y"])))
		end)
	end

More stuff here: How to make part "collide" when dragged by mouse - #12 by PANDASonNOOB

2 Likes

There’s a lot of tutorials on this, have you tried searching in the DevForum or Google/YouTube/TikTok/YouGotWhatIMean

yes, idk what I should use to search (like what search term)

Search something like:
Placement system

The vaguer, the bettter, but not too vague or you will get something you will not need, try putting something also like in filters like solved, so you know you will find the answer

1 Like

I feel stupid (ig im bad at searching, I spent a long time looking around and all the stuff I found had the same issue)

You would need to raycast for this,
In which you would do Raycast.Position + Raycast.Normal

which should Prevent “non collision” from happening

2 Likes

But I’m not tho.

I literally am bad at doing research and math.

(btw say this in a Message, not a thread)

NVM I FORGOT TO MENTION:
theres rotation here, so a simple offset wont work ;-;

spent like an hour searching around for the offsets and stuff and then I remember I had rotations which makes the offset not work if you rotate the block

basically:


looks like it worked right?

oops its in the floor again

then theres this

new script (yes this is stupid and its just if statements lol)

if dum:IsA("BasePart") then
		place = run.RenderStepped:Connect(function()
			if mouse.TargetSurface == Enum.NormalId.Top then
				dum.CFrame = CFrame.new(mouse.Hit.Position.X, (mouse.Target.Position.Y +(mouse.Target.Size.Y/2)) + dum.Size.Y/2, mouse.Hit.Position.Z) * CFrame.Angles(math.rad(rot["r"]), math.rad(rot["t"]), math.rad(rot["y"]))
			elseif mouse.TargetSurface == Enum.NormalId.Bottom then
				dum.CFrame = CFrame.new(mouse.Hit.Position.X, (mouse.Target.Position.Y -(mouse.Target.Size.Y/2)) - dum.Size.Y/2, mouse.Hit.Position.Z) * CFrame.Angles(math.rad(rot["r"]), math.rad(rot["t"]), math.rad(rot["y"]))
			elseif mouse.TargetSurface == Enum.NormalId.Right then
				dum.CFrame = CFrame.new((mouse.Target.Position.X +(mouse.Target.Size.X/2)) + dum.Size.X/2, mouse.Hit.Position.Y, mouse.Hit.Position.Z) * CFrame.Angles(math.rad(rot["r"]), math.rad(rot["t"]), math.rad(rot["y"]))
			elseif mouse.TargetSurface == Enum.NormalId.Left then
				dum.CFrame = CFrame.new((mouse.Target.Position.X -(mouse.Target.Size.X/2)) - dum.Size.X/2, mouse.Hit.Position.Y, mouse.Hit.Position.Z) * CFrame.Angles(math.rad(rot["r"]), math.rad(rot["t"]), math.rad(rot["y"]))
			elseif mouse.TargetSurface == Enum.NormalId.Back then
				dum.CFrame = CFrame.new(mouse.Target.Position.X, mouse.Target.Position.Y, (mouse.Target.Position.Z +(mouse.Target.Size.Z/2)) + dum.Size.Z/2) * CFrame.Angles(math.rad(rot["r"]), math.rad(rot["t"]), math.rad(rot["y"]))
			elseif mouse.TargetSurface == Enum.NormalId.Front then
				dum.CFrame = CFrame.new(mouse.Target.Position.X, mouse.Target.Position.Y, (mouse.Target.Position.Z -(mouse.Target.Size.Z/2)) - dum.Size.Z/2) * CFrame.Angles(math.rad(rot["r"]), math.rad(rot["t"]), math.rad(rot["y"]))
			end
		end)
	else

Is it a part or model? If it’s a part, you can add + dum.Size.Y / 2

But, since you need to rotate it, you can’t do that. Your best bet is to convert the size into some sort of object space value, make it absolute, and then add it according to the orientation. I remember making something like this for a different cause, but I’m not sure how this would apply to it.

Another solution is to be lazy and call some parts in part function and move it up until it doesn’t have any.

It could be a part or a model
How would I make the value absolute when the part could have a different length then height?

I was thinking using pythagorean theorem to find the length between the midpoint of the part/model and the lowest point (or highest, depending on which face its on), but the problem is, I cant find the lowest point anyways

Just realized I wouldn’t even need any theorem since it’d always be a straight line parralel to either X, Y, or Z axis

YOO DIS WORKS!!!
(I used the function from here: How to get the lowest point of a part - #12 by Darkmist101 )

Nvm, it can only get the correct location for one side, so if im placing at a corner, the block would still be in the ground on the two other side ;-;

raycast from all sides/directions and position from there.

Maybe you could give this a try: Absolute size of a part - #22 by nicemike40

This will get you the AABB of the part. I.e. the bounding box that is upright abd not rotated. Then, all you need to do is to add half the size of the Y axis, and in the future whatever other axis you need when placing it on walls for eg.

But wouldn’t I need the rotated one since otherwise it could still be in the ground since the bounding box doesn’t think its in the ground?

oh wait I understood that wrong

Yea ill give that a try!