Change the orientation of an object based on where it is

I want to make it so when the player deploys an explosive device, it faces the players, which ive managed to do pretty well,
One issue that ive encounter is that the exploisve device clips through walls as it doesnt rotate to the walls angle, same with a ceilling

How could I make it rotate to the angle thats its placed onto?

the script that has the orientation changing things
local RS = game:GetService("ReplicatedStorage")

local RemoteEvent = RS.ClientEvents.C4Event
local Players = game:GetService("Players")

RemoteEvent.OnServerEvent:Connect(function(Player, MousePosition)
	local Char = Player.Character or Player.CharacterAdded:Wait()
	local C4 = game.ServerStorage.CloneC4:Clone()
	
	C4.Parent = game.workspace
	C4.Name = Player.Name.. "'s C4"
	C4.Handle.Position = MousePosition + Vector3.new(0,0.235,0)
	
	local CurrentPosX = C4.Handle.Orientation.X
	local CurrentPosY = C4.Handle.Orientation.Y
	C4.Handle.Orientation = Vector3.new(CurrentPosX,Char.HumanoidRootPart.Orientation.Y,CurrentPosY)
end)

here is a video example of what i mean

Consider learning CFrames. They hold position and orientation values, and have a lot of manipulation methods.

Yes but, how could I get it to rotate to the correct angle a side of a part is?

So you want the explosive to basically sit on the surface the correct way?

Yeah

like this
image

Yes, I was thinking about how to do this for 40 minutes. I just thought of a good solution, so I’ll be coding that.

I wrote this script, however I don’t think it will work that well.

local explosive: Model
local hit: BasePart

-- assuming this script is being run when you want to anchor and position the explosive

explosive.PrimaryPart.Anchored = true

--\\ CFrame Side Math

local c = hit.CFrame
local s = hit.Size / 2
local p = c.Position
local sides = {
	Front = c.LookVector * s.Z + p,
	Back = -c.LookVector * s.Z + p,
	Right = c.RightVector * s.X + p,
	Left = -c.RightVector * s.X + p,
	Top = c.UpVector * s.Y + p,
	Bottom = -c.UpVector * s.Y + p,
}

local touchedside, magnitude = 1000
local expos = explosive:GetPivot().Position

--\\ Get Touched Side (ideally)

for side, pos in pairs(sides) {
	local dist = (expos - pos).Magnitude
	if dist < magnitude then
		magnitude = dist
		touchedside = side
	end
}

--\\ Set Position of Explosive

local sidepos = touchedside.Position
local lookc = CFrame.new(sidepos, hit.Position) * CFrame.Angles(0,math.pi,0)
explosive:PivotTo(lookc)

I didn’t want to just throw it out, so here it is. Again, I don’t think it will work. I imagine the explosive will go to the middle of the surface instead of, you know, where it should be.

Maybe try using a PlaneConstraint. This constraint isn’t documented too much, but it simply takes two points and creates a plane between them. This plane is the only space that the object can stay in, and this would be the surface it touches. You might be able to use part of the code I sent to get the touched surface, although it might require some fixing since I can’t test it right now. There might also be a better way.

Dont really understand how I incorporate this into my current script, theres also a few warnings on some lines