I've struggled with this issue for over a week now and my project has halted completely. The mouse object is giving a wrong object as its target whenever I click. I made this little video demonstrating the issue, alongside with the exact place in this video.
In my original project I have tried different ways to mimic the Target property, none of them has worked.
function mouse:getTarget(directory)
-- local ray = Ray.new(realCamera.CFrame.p,realMouse.Origin.lookVector * 2000)
-- local part = workspace:FindPartOnRay(ray)
-- drawRay(ray)
-- return part
-- local mouseRay = realMouse.UnitRay
-- local ray = Ray.new(mouseRay.Origin,mouseRay.Direction * 2000)
-- local part = workspace:FindPartOnRay(ray)
-- drawRay(ray)
-- return part
-- I guess I just gotta experiment: use WorldToViewportPoint on all clickable objects and check if the mouse is on it on the xy-plane, if multiple ones are found it gives the one with the shortest distance
-- directory = directory or workspace
--
-- local closest,dis = nil,math.huge
-- local children = directory:GetChildren()
-- for n = 1,#children do
-- local child = children[n]
-- if child:IsA("BasePart") then
-- local viewportPoint,isVisible = realCamera:WorldToViewportPoint(child.CFrame.p)
-- if isVisible then -- else just ignore
-- local delta = Vector2.new(viewportPoint.X - realMouse.X, viewportPoint.Y - realMouse.Y).magnitude
-- if delta < dis then
-- closest = child
-- dis = delta
-- end
-- end
-- end
-- end
-- return closest
end
Where "mouse" is a custom class and realMouse the actual PlayerMouse object. The reason I cannot really use the Target property even if this issue didn't exist, is because its distance is too limited for my needs. I would be using the Ray solution but the mouse's Hit property is also giving incorrect results, which must be connected to the Target issue.
I believe the camera's Focus property is causing this all but I have no idea why. This is how I'm updating the camera, as well as setting the Focus property:
function camera:update()
if self.subject then
realCamera.CFrame = self.subject.CFrame * self.rotation * CFrame.new(0,0,self.distance)
realCamera.Focus = realCamera.CFrame
end
end
