I need to create a system that allows my character to look at the mouse pos from the screen regardless of the camera orientation

  1. What do you want to achieve? Keep it simple and clear!
    I am making a topdown rpg game and i want my character to look towards the cursor position on the screen, it works untill you rotate the camera in which it looks towards a completely different position

  2. What is the issue? Include screenshots / videos if possible!

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I think the issue is with CFrame.Angles() but im not quite sure how i would go about it

This is the section of the code responsible for the camera

---- Integers ----
local Tilt = 1
local FullCircle = math.pi * 2

---- Boolean ----
local PointToMouse = true

local Center = Vector2.new(workspace.CurrentCamera.ViewportSize.X / 2, workspace.CurrentCamera.ViewportSize.Y / 2  - (game:GetService("GuiService"):GetGuiInset().Y/2))

---- Functions ----
local function CameraHandler()
	local Angle = Index.Value * (FullCircle / 100)
	local X = math.cos(Angle) * -15
	local Z = math.sin(Angle) * -15
	
	local DefaultCFrame = CFrame.new(Root.Position + Vector3.new(X, Zoom.Value, Z), Root.Position)
	Camera.CFrame = DefaultCFrame * CFrame.Angles(math.rad( ((Mouse.Y - Mouse.ViewSizeY / 2) / Mouse.ViewSizeY) * -Tilt ), math.rad(((Mouse.X - Mouse.ViewSizeX / 2) / Mouse.ViewSizeX) * -Tilt), 0)
	
	if PointToMouse == true then
		local MousePos = game.UserInputService:GetMouseLocation() - game:GetService("GuiService"):GetGuiInset()
		local ToLook = (Center - MousePos).Unit
		
		TweenService:Create(Root, TweenInfo.new(0.15), {CFrame = CFrame.new(Root.Position, Vector3.new(Root.Position.X + ToLook.X * 1, Root.Position.Y, Root.Position.Z + ToLook.Y * 1)) * CFrame.Angles(math.rad(0), math.rad(90), math.rad(0))}):Play()
	end
	
end

I made something similar a few months ago: (Put in a renderstepped loop)

if (game:GetService("Workspace").CurrentCamera.CFrame.Position - Character:WaitForChild("Head").Position).Magnitude > 1 then
				
		local RootPosition, MousePosition = HumanoidRootPart.Position, Mouse.Hit.Position

		HumanoidRootPart.CFrame = CFrame.lookAt(RootPosition, Vector3.new(MousePosition.X, RootPosition.Y, MousePosition.Z))
			
end

It just uses CFrame.LookAt to make the HRP look at the mouses X and Z position. The if statement check might not be needed idk.

1 Like

Maybe try using mouse.Hit.Position instead? Or change some things in the default roblox CameraScript. Also, I recommend using alignorientation as it’s smoother for things like characters looking at vector3s.

mouse.Hit isnt as accurate as the method im currently using, if you move your mouse close to the character it doesnt rotate properly, i assume that is due to the target filter but i cant remove it as it would lead to some weird behaviour when putting the mouse over objects that have different Y positions

It seems to be working fine for me no matter how close / far away the mouse is. I think something of yours may be causing the issue.

yeah just confirmed it was indeed the target filter but like i said, if i remove it it leads to this kind of behaviour which i didnt really want

ig it could be solved if i were to filter out all the walls and such but i think that would still happen in maps with multiple floors, not mentioning the pain it would be to filter everything out

nvm fixed, all i had to do was multiply the 90 degree angle by - 3.6 * Index

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