Get models to rotate along walls using rays

This is what I currently have for my model placement.

Mouse.Move:Connect(function()
	if IsMoving then			
		for i, v in pairs(Model:GetChildren()) do
			v.CanCollide = false
		end
			
		local UnitRay  = Camera:ScreenPointToRay(Mouse.X, Mouse.Y, 1)
		local NewRay = Ray.new(UnitRay.Origin, UnitRay.Direction * 1000)
		local Hit, Pos, Normal = workspace:FindPartOnRayWithIgnoreList(NewRay, {Model, Player.Character})
		
		local PosX = math.floor((Pos.X / 2) + 0.5) * 2
		local PosY = Pos.Y
		local PosZ = math.floor((Pos.Z / 2) + 0.5) * 2
		
		local NewPos = Vector3.new(PosX, PosY, PosZ)
--[[
		Model:SetPrimaryPartCFrame(
			CFrame.new(NewPos, NewPos + Normal * 15) * 
			CFrame.Angles(-math.rad(90), math.rad(Rotation), 0) * 
			CFrame.new(0, Model.PrimaryPart.Size.Y / 2, 0)
		)
]]
		Model:SetPrimaryPartCFrame(
			CFrame.new(NewPos) *
			CFrame.Angles(0, math.rad(Rotation), 0) *
			CFrame.new(0, Model.PrimaryPart.Size.Y / 2, 0)
		)
	end
end)

As you can see, I have 2 Model:SetPrimaryPartCFrame chunks of code. The one commented out is what I originally had, which works with wall placement, but it screws up my rotation. Then the SetPrimaryPartCFrame that’s actually being run works with my rotation, but if I move a part along the wall it just stays upright instead of rotating to the surface.

This is how it SHOULD look
com-video-to-gif%20(1)

This how it currently looks
com-video-to-gif

As you can see, the model is staying upright, instead of rotating along the walls surface.

I know this has something to do with Pos + Normal * 15 but I can’t just do

Model:SetPrimaryPartCFrame(
	CFrame.new(NewPos, NewPos + Normal * 15) * -- SHOULD make the model rotate along the wall + move it to follow the mouse
	CFrame.Angles(0, math.rad(Rotation), 0) * -- Rotates the model based on its current Rotation
	CFrame.new(0, Model.PrimaryPart.Size.Y / 2, 0) -- Offsets the model so it isn't halfway thru the ground
)

As that just screws with my rotation

CFrame.Angles(0, math.rad(Rotation), 0)
2 Likes