How do I make it so my painting does not go past the wall?

Hello everyone, so I have this placement script that moves a painting on the wall where the mouse is pointing. This is some of the code for it:

local currentRotation = model.PrimaryPart.Orientation.Z -- Changes the rotation to its relative current item's rotation
				
model:SetPrimaryPartCFrame((CFrame.new(math.round(mouse.Hit.X), math.round(mouse.Hit.Y), math.round(mouse.Hit.Z + 0.6)) * (CFrame.Angles(0,math.rad(180),math.rad(currentRotation)))))

So this works as intended, the painting goes to where the mouse is pointing on the wall but I would like for it to not clip past the wall or in other ways stay completely within the boundaries of the wall and not have the other half go past the wall. Do you know how I can accomplish this? Any formulas? Thanks!

1 Like

Anyone still might be able to help me? Thanks

use raycast, it gives u the exact point of intercept as well as the normal which u can use to make the painting flat on whatever you’re placing it

You can change the pivot of the painting model to an appropriate place, since normally it is inside the model, which causes it to clip the wall.

Further elaborating on what I said earlier:

Mouse.Hit is actually pretty bad imo
Instead, what I would do is to get the Vector2 position of the mouse on the screen and then convert it into a ray using camera:ScreenPointToRay()
Using the ray’s information, perform a raycast

What is pivot? I’ve never used it before. Thanks

model:SetPrimaryPartCFrame(cframe) is deprecated, please use this instead and test, and like what @thighwinder said, please use workspace:Raycast()

model:PivotTo((CFrame.new(math.round(mouse.Hit.X), math.round(mouse.Hit.Y), math.round(mouse.Hit.Z + 0.6)) * (CFrame.Angles(0,math.rad(180),math.rad(currentRotation))))

Now to finally address the issue of clipping, you could put an attachment inside the painting where u want the painting to be attached to the wall
And when pivoting the model to the CFrame, consider the attachment’s objectspace CFrame

Pivot is an item which can be used for moving models and rotate differently, The API is:

  • PVInstance:PivotTo() The Replacement for Model:SetPrimaryPartCFrame()

  • PVInstance:GetPivot() The Replacement for Model:GetPrimaryPartCFrame()

1 Like

Thanks everyone, but somehow I figured it out using ratios.

1 Like