Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

What is the best way to detect a mouse's whereabouts (for 3D use)?

Asked by 9 years ago

What I mean is, imagine you have a block far far away, what would be the best way to detect whether my mouse runs over that block or something like that?

Any help will do, thanks!

0
LocalScript, obviously, but Players.LocalPlayer:GetMouse() returns a PlayerMouse object you can use to get the info you need. adark 5487 — 9y

2 answers

Log in to vote
1
Answered by
dyler3 1510 Moderation Voter
9 years ago

You can use the properties within the players mouse to get things like this. Some useful properties and methods for getting the mouse are as follows:


Properties

Mouse.Hit

- The position of where the mouse is pointing.

Mouse.Target

- The current object the mouse is over. If the mouse is not over anything, this property is nil

Mouse.TargetSurface

- The surface of the mouse's target that it is hitting.

Mouse.TargetFilter

- If set, the TargetFilter and its descendants will be ignored when determining which part the mouse is pointing to.

Mouse.X

- The 2-Dimensional X Position of the mouse on the screen

Mouse.Y

- The 2-Dimensional Y Position of the mouse on the screen

Click here for more properties


Getting The Mouse

In order to get the mouse, you need to first find the player whose mouse you are attempting to get. We can do that multiple ways, but here are just a couple common methods:

In a LocalScript (Descendant from the player)

local Mouse = game.Players.LocalPlayer:GetMouse()

In a server script descendant from the Workspace

game.Players.PlayerAdded:connect(function(Player)
    local Mouse = Player:GetMouse()
end)

If you have any further problems/questions, please leave a comment below, and I'll see what I can do. Hope I helped :P

0
You can't get a mouse from the Server - it only works from Clients. adark 5487 — 9y
0
#wrekt User#5978 25 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

You can raycast using the player's mouse's position (which you can get using the UserInputService, specifically the InputBegan/InputChanged/InputEnded events) with the method Camera.ScreenPointToRay.

A downside to this method is that you will not be able to detect blocks that are further than 1000 studs away (or 999.5 if you want to be safe from rounding errors - otherwise Roblox may complain that it's magnitude is greater than 1000).

An advantage of this method is that you can manipulate the ray and raycast in different directions (ex 1 degree to the left), if for some reason you want/need to do this.

Answer this question