Attach Object to Surface Normal while Maintaining Other Rotations

So I have a poster movement system so that you can move the posters around freely, and they’ll reattach themselves to the walls using the normal of the wall. However, this means it doesn’t keep the rotation it had when attaching and gets oriented different than to how I need it to be.

Here’s a video showing the issue:
robloxapp-20231217-1703589.wmv (3.2 MB)
I don’t know how to translate the object’s rotation so that it lays flat on the surface while maintaining some parts of its rotation. I’ve tried multiplying it by different parts of its rotations, but different surface angles means I have to use a different orientation consistently.

Any ideas on how to tackle this issue?

Here’s the video since it didn’t embed properly in the original showing my issue.

Could you show your code?

Sure, I updated it some to help my issue some but now the orientation is weird and it’s directly related to the orientation of the object below. I can’t remove the orientation of the object away from the poster as the poster wouldn’t be able to be put on rotated parts from my testing, along with other weird behavior.
Here’s a video showing the new behavior:


Here’s the code for the actual physical placing it:

Could you paste the code instead of screenshotting it?

if object:HasTag("Furniture") then
			local raycastParams = RaycastParams.new()
			raycastParams.RespectCanCollide = true
			raycastParams.FilterDescendantsInstances = {object,player.Character}
			local ray = workspace:Raycast(cameraCFrame.Position,cameraCFrame.LookVector*10,raycastParams)
			if ray then
				object.PrimaryPart.CFrame = CFrame.new(ray.Position, ray.Position+ray.Normal) * CFrame.Angles(0,0,math.rad(object.PrimaryPart.Rotation.Z - 180))
				object.CFrameValue.Value = object.PrimaryPart.CFrame
			else
				object.PrimaryPart.CFrame = object.CFrameValue.Value
			end
			
			object.PrimaryPart.Anchored = true
		end

Sorry, there you go. I use this script for dropping any object in general and not just furniture, so that’s why it requires the furniture tag.

This should work, all I did was make the Y orientation the same, which should solve the problem. It depends on the orientation of your paper though.

Code:

if object:HasTag("Furniture") then
	local raycastParams = RaycastParams.new()
	raycastParams.RespectCanCollide = true
	raycastParams.FilterDescendantsInstances = {object,player.Character}
	local ray = workspace:Raycast(cameraCFrame.Position,cameraCFrame.LookVector*10,raycastParams)
	if ray then
		object.PrimaryPart.CFrame = CFrame.lookAlong(ray.Position, ray.Normal) * CFrame.Angles(0, 0, object.PrimaryPart.CFrame.Orientation.Z)
		object.CFrameValue.Value = object.PrimaryPart.CFrame
	else
		object.PrimaryPart.CFrame = object.CFrameValue.Value
	end

	object.PrimaryPart.Anchored = true
end
1 Like

That works perfectly, thanks you so much!
Here’s the finished code:

if object:HasTag("Furniture") then
			local raycastParams = RaycastParams.new()
			raycastParams.RespectCanCollide = true
			raycastParams.FilterDescendantsInstances = {object,player.Character}
			local ray = workspace:Raycast(cameraCFrame.Position,cameraCFrame.LookVector*10,raycastParams)
			if ray then
				object.PrimaryPart.CFrame = CFrame.lookAlong(ray.Position, ray.Normal) * CFrame.Angles(0,0,object.PrimaryPart.Orientation.Z)
				object.CFrameValue.Value = object.PrimaryPart.CFrame
			else
				object.PrimaryPart.CFrame = object.CFrameValue.Value
			end
			
			object.PrimaryPart.Anchored = true
		end
1 Like

No problem, a solution would be appreciated as thanks though. If you have any more questions, feel free to ask.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.