How would I be able to make the player look at the location of the GUI ImageLabel on the screen?

I am trying to make the player look in the direction of the crosshair UI that is tweening towards the mouse’s position on the screen, however I haven’t been able to find examples of this online.
It currently only looks at the mouse position since I am using a basic script for that. I didn’t want to have the crosshair as the mouse cursor as I want the rotation of the player and the position of the cursor to be smooth rather than snappy.

I’ve seen it being done before in various games and have considered using a part that follows the mouse for the player to look at or a BodyGyro, but I want it to look exactly at the UI following the mouse.

It’s for a top-down game and the crosshair requires that the player be looking directly at it.

I think in the example you show, the player is always at the center of the screen.
So the code would see which direction (angle) the cursor is from the center of the screen, then
apply that angle to a body gyro or something to rotate the player to that direction.

There might be better ways to do this, but just thinking about it, I would create a cframe at origin, and have it look towards the position of the mouse x,y normalized, then get that roation from the created cframe.

Here is a place I made
WalkToCursor.rbxl (54.7 KB)
image

local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable

local player = game.Players.LocalPlayer
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local hrp = character.PrimaryPart
local mouse = player:GetMouse()
local GuiService = game:GetService("GuiService")
local guiInset = GuiService:GetGuiInset()

local attachment0 = Instance.new("Attachment")
attachment0.Name = "AlignOrientationAttachment0"
attachment0.Parent = hrp

local gyro = Instance.new("AlignOrientation")
gyro.Mode = Enum.OrientationAlignmentMode.OneAttachment
gyro.MaxTorque = math.huge
gyro.Responsiveness = math.huge
gyro.Parent = hrp
gyro.Attachment0 = attachment0

local mouseDown = false
mouse.Button1Down:Connect(function()
	mouseDown = true
end)
mouse.Button1Up:Connect(function()
	mouseDown = false
end)

game["Run Service"].Heartbeat:Connect(function()
	camera.CFrame = CFrame.lookAt(Vector3.new(hrp.Position.X,24,hrp.Position.Z+2),hrp.Position,Vector3.yAxis)
	
	
	local viewSize = workspace.CurrentCamera.ViewportSize
	local x = (viewSize.X/2) - mouse.X
	local y = (mouse.Y + guiInset.Y) - (viewSize.Y/2)

	
	local cf = CFrame.lookAt(Vector3.zero,Vector3.new(x,0,y),Vector3.yAxis)
	attachment0.CFrame = cf
	
	if mouseDown then
		humanoid:MoveTo(hrp.Position + (Vector3.new(-cf.LookVector.X,0,cf.LookVector.Z) * 2 ))
	end
end)

Thanks for the response. I have some code that makes a different ImageLabel rotate in the direction of the cursor. How should I go about converting the angle of this new GUI into the rotation of the player?

Actually, can you tell me how you made the ImageLabel face the cursor?
Thanks!