Rotate object vertically dependent on mouse target

I’m currently working on a free model.

In the free model you have a C4 which you can place wherever you want and later detonate it by simply pressing a key. The issue I have is that when I try to place the C4 on a wall it obviously does not rotate accordingly and bugs into it.

What I want to achieve is that where ever I put my mouse at, the script can somehow detect the orientation of the mouse.Target and then rotate the C4 in an automated process approximately to it. I don’t need it to be perfect for spheres but when there’s a wall I would like to have it rotated 90 degrees.

Reference Video: https://youtu.be/88g7_4dPRK0

This is the code used to determine the position of the C4 (LocalScript):
Edit: This is not the whole script but these are the necessary parts.

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local status = false

local tool = script.Parent

local function placingFire_Event()
	local pos = mouse.Hit.p + Vector3.new(0,.4,0)
	script.Parent.placeEvent:FireServer(pos)
	status = true
end

tool.Activated:Connect(function()
	local distChar = plr:DistanceFromCharacter(mouse.Hit.p)
	if not hasDistLimit then
		placingFire_Event()
		return
	end
	if distChar <= distLimit then
		placingFire_Event()
	else
		if plr.PlayerGui:FindFirstChild("overLimit") then return end
		local uc = script.Parent.overLimit:Clone()
		uc.Parent = plr.PlayerGui
		uc.Enabled = true
		wait(3)
		uc:Destroy()
	end
end)

This line is responsible for setting the C4s position and as already mentioned, I do not modify the orientation in any way because I just have no clue how to.

local pos = mouse.Hit.p + Vector3.new(0,.4,0)

If you need it, the free model can be found here.

Any help is highly appreciated!

You should be able to get the object’s orientation with mouse.Target.Orientation. To match the orientation you could save it as a variable and apply it to the model.

How about you try raycasting as the raycast will return the surface normal and you can use that to create a rotated crframe

Now it does indeed rotate but not properly.
As example I did not apply the mouse.Target.Orientation to the C4 tracking the cursor, just to the onces that get placed.

Reference Video: https://youtu.be/2kapf0FxhoY

Changed code:

local script:

local function placingFire_Event()
	local pos = mouse.Hit.p + Vector3.new(0,.4,0)
	local orientation = mouse.Target.Orientation
	print(orientation)
	script.Parent.placeEvent:FireServer(pos, orientation)
	status = true
end

script handling the place remote event:

local part = game:GetService("ReplicatedStorage"):WaitForChild("Models"):WaitForChild("C4"):Clone()
	part.CanCollide = true
	part.Anchored = true
	part.Parent = workspace.Placings
	part.Name = plr.Name..".C4"
	part.Position = pos
	part.Orientation = Vector3.new(orient)

(only necessary parts of script again)
It definitely changed something though!

Is there a documentation about this or something. I have used raycast before but not excessively enough to know a lot about it. How exactly would that work?

Hm so basically you can cast a unityray from your camera at the positions of your mouse, and then create a raycast using the unityray origin and direction in a 3D space

local unityray = camera:ScreenPointToRay(mouse.X, mouse.Y)
local raycast = workspace:Raycast(unityray.Origin, unityray.Direction * 100, params)

Sorry, I’m tired and forgot objects could be scaled along the x or z axis. Here is the documentation for raycasting:

Raycasting | Documentation - Roblox Creator Hub

I just did that and it seems that the exact same thing happened just when I tried the mouse.Target.Orientation method suggested by PigTPM_S3.

Reference Video: https://youtu.be/2kapf0FxhoY

It rotates the part but not properly. And I printed out the orientation. Everything seemed correct.

Is there a chance this line causes the problem in the ServerScript. Can I just set the orientation of the C4 to the orientation of the wall or am I missing something?

part.Orientation = Vector3.new(orient)
-- part = the C4 being placed
-- orient = "raycast.Instance.Orientation"

Yeah because you have to construct a new cframe using the surface normal vector, the surface normal is a vector that points perpendicular to the surface

This might sound dumb but: how do I achieve this? heh

1 Like

So I tried this out but to no avail. I got it to work on the wall but then it was bugging on the floor. It’s very late and in addition to this that exhausted me really. Perhaps if someone could provide me with the right calculations which I can then implement into my script, I would appreciate it.

1 Like