Strange mouse behaviour

The code below is meant to return a position in 3D space, endPosition. It usually does return a position in 3D space, however it is never on target, and when I test it out, it looks as if it has been hashed into some random position.

local mouseLocation = UIS:GetMouseLocation()
local unitRay = camera:ScreenPointToRay(mouseLocation.X, mouseLocation.Y)
local raycast = workspace:Raycast(unitRay.Origin, unitRay.Direction * 2000, params)
			
local endPosition = raycast.Position

Is there anything wrong with my code that could cause this to happen? There are no errors, only a very incorrect endPosition.

image

I clicked where the cursor is in the image, however the part ended up far below it. The part’s position is endPosition.

if you’re just trying to get the location of the mouse in 3D space, you can do

local Players = game:GetService("Players")
local mouse = Players.LocalPlayer:GetMouse()

local pos = mouse.Hit

if that’s not what you’re trying to do, let me know - its sort of difficult to tell what you’re actually trying to do if not that

1 Like

This works well! I was trying to avoid using GetMouse, as Roblox has been talking about how they highly recommend to use User Input Service on their documentation instead, but this works way better! Thanks!

1 Like

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