How to get Image Label's Vector 3 position as you would with mouse.hit.p?

Hello. I am currently working on a third person shooter game I need some assistance on how I would go about doing the following. As of right now, the players cursor is locked in the center of the screen but the camera is offset to make it look like it is above their shoulder. I do not like the position at which the mouse is at so I decided to use an Image label for the crosshair. My problem is, I need to know the crosshair(image label)'s vector 3 position just like i normally would using the mouse so that the guns work. I am not quite sure how to do this. I looked into Screen Point to Ray after looking at the opposite; world to screen point but had no luck. In summary, what would be the best way to get an Image Label’s Vector 3 position as you would with mouse.hit.p? Thanks for reading!

1 Like

I’m confused, do you mean you want to cast a ray from a given point on your screen to a given Vector3?

If so, you can use ScreenPointToRay to do this.

local userInputService = game:GetService('UserInputService')
local mouse = game:GetService('Players').LocalPlayer:GetMouse()

local camera = workspace.CurrentCamera

userInputService.InputBegan:Connect(function(input, isSystemReserved)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local unitRay = camera:ScreenPointToRay(mouse.X, mouse.Y) -- this is where we're casting the ray from. it can be any vector2, since you want to use UI elements, use the AbsolutePosition.X and AbsolutePosition.Y values
		local rayResult = workspace:Raycast(unitRay.Origin, unitRay.Direction * 1000) -- since the above returns a unit ray, we have to make it longer (1000 studs for example)
		if rayResult then
			local part = Instance.new('Part')
			part.Position = rayResult.Position -- this would be the vector3 returned from the ray
			part.Parent = workspace
			part.Anchored = true
		end
	end
end)
1 Like

Thanks for replying! What i mean to do is not use the mouse position to cast a ray but rather the Crosshair Image Label. The cursor would be disabled in this case.

You can cast the ray from the label’s AbsolutePosition instead of the mouse’s position.

2 Likes

Can you explain the difference between the Image Label position and absolute position?

The AbsolutePosition is the distance from 0,0 in pixels which would give you the maximum amount of accuracy.

2 Likes

Alright thanks I will check it out.

1 Like