[HELP] Strugling To Rotate A Mesh To Face The Mouse

Hello everybody , i’ve been really strugling with this for quiet a while trying to rotate a mesh (sword) to face the mouse. Now firstly just before we talk about this i just wanna say that the mesh that i got from the toolbox has weird Look ,Up and Right vectors idk if it’s normal or not :

And here is the mesh’s X Y and Z axis because i use sword.CFrame:ToObjectSpace(mouseHit)

I know this thanks to a plugin anyways now that is out of the way let’s talk about what i have achieved so far i got the Y axis working properly it faces the mouse just as intended :

Here’s the code that i used for this :

mouseRelativeToSword = sword.CFrame:ToObjectSpace(mouseHit) 

sword.CFrame = sword.CFrame * CFrame.Angles(0,-math.atan2(mouseRelativeToSword.Position.Z,mouseRelativeToSword.Position.X) + math.rad(180),0)

You might be wondering why i used + 180 degrees it’s because like i said the mesh is kinda weird and when i remove the 180 degrees the back of the sword is the one that faces the mesh like this

But the real problem is the Z axis i want it to move up and down depending on the mouse’s position

I tried doing this :

sword.CFrame = sword.CFrame * CFrame.Angles(0,-math.atan2(mouseRelativeToSword.Position.Z,mouseRelativeToSword.Position.X) + math.rad(180),-math.atan2(mouseRelativeToSword.Position.Y,mouseRelativeToSword.Position.X))

Now this works partially but when aiming at certain places it just bugs and it rotates incorectly

Here’s the full code :

local replicatedStorage = game:GetService("ReplicatedStorage")
local darkEtherEvent = replicatedStorage.AbilityEvents.SwordEvents:WaitForChild("DarkEtherEvent")

local raycastModule = require(replicatedStorage.Modules.ClientCast)

local TweenService = game:GetService("TweenService")

local Debris = game:GetService("Debris")

local debounce = {}

local portal = nil
local mouseRelativeToSword = nil
darkEtherEvent.OnServerEvent:Connect(function(player,Task,mouseHit)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
	
	if Task == "Open Sword Portal" then
		portal = game.Workspace.SwordPortal:Clone()
		portal.Anchored = false
		portal.CFrame = humanoidRootPart.CFrame * CFrame.new(4,3,2) * CFrame.Angles(math.rad(90),0,0)
		portal.Parent = game.Workspace
		
		local weld = Instance.new("WeldConstraint",portal)
		weld.Part0 = portal
		weld.Part1 = humanoidRootPart
	else
		local sword = replicatedStorage.Assets.DarkEtherSword:Clone()
		sword.Anchored = true
		sword.Position = portal.Position
		sword.Parent = game.Workspace
		
		mouseRelativeToSword = sword.CFrame:ToObjectSpace(mouseHit) 

		sword.CFrame = sword.CFrame * CFrame.Angles(0,-math.atan2(mouseRelativeToSword.Position.Z,mouseRelativeToSword.Position.X) + math.rad(180),0)
		
		--WORKING Y AXIS --> sword.CFrame = sword.CFrame * CFrame.Angles(0,-math.atan2(mouseRelativeToSword.Position.Z,mouseRelativeToSword.Position.X) + math.rad(180),0) 
	end
end)

Any help would be greatly appreciated.

GreenGuyGaming FOUND A SOLUTION :
sword.CFrame = CFrame.lookAt(sword.Position, mouseHit) * CFrame.Angles(0, -math.pi / 2, 0)

1 Like

Instead of all this:

mouseRelativeToSword = sword.CFrame:ToObjectSpace(mouseHit) 

sword.CFrame = sword.CFrame * CFrame.Angles(0,-math.atan2(mouseRelativeToSword.Position.Z,mouseRelativeToSword.Position.X) + math.rad(180),0)

Why don’t you just…

sword.CFrame = CFrame.lookAt(sword.Position, mouseHit) * CFrame.Angles(0, -math.pi / 2, 0)
1 Like

I actually tried this method though it doesn’t plus i wanna get better at trigonometry and it’s just painful

EDIT : it actually works i just didn’t the same thing that you did oopsie (i feel kinda stupid)

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