Mouse

Hiển Thị Bản Đã Lỗi Thời

*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.

Không Thể Tạo

Chuột đã bị thay thế bởi UserInputServiceContextActionService , có phạm vi rộng hơn, có nhiều tính năng hơn và hỗ trợ tốt hơn các mô hình chéo nền tảng .Nó vẫn được hỗ trợ do sử dụng rộng rãi của nó, nhưng bạn nên xem xét mạnh mẽ sử dụng các lựa chọn thay thế này.

Vật phẩm Chuột bao gồm các API khác nhau cho các điểm, chủ yếu là cho nút và phát bắn tia.Nó có thể được truy cập thông qua Player:GetMouse() được gọi trên Players.LocalPlayer trong một LocalScript .Nó cũng được truyền bởi sự kiện Tool.Equipped .

  • Nó nổi bật nhất với thuộc tính Icon, thay đổi sự xuất ngoại hìnhcủa con trỏ.
  • Nó liên tục phát vị trí chuột màn hình vào thế giới 3D bằng cách sử dụng thuộc tính TargetFilter, lưu kết quả của raycast trong Hit, TargetTargetSurface tính chất.Chúng có thể hữu ích cho các trường hợp đơn giản, nhưng WorldRoot:Raycast() nên được sử dụng trong các trường hợp phức tạp hơn raycasting .
  • Plugins có thể sử dụng Plugin:GetMouse() để có được PluginMouse , có hành vi tương tự.

-- Từ một LocalScript:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
-- Đặt biểu tượng chuột
mouse.Icon = "rbxasset://SystemCursors/Wait"

Ghi chú:

  • Vật này không kiểm soát/hạn chế chuyển động điểm. Đối với việc này, xem UserInputService.MouseBehaviorUserInputService.MouseDeltaSensitivity .

  • Nếu hai chức năng được kết nối với cùng một sự kiện nhập, chẳng hạn như , cả hai chức năng sẽ chạy khi sự kiện bắt lửa.Không có khái niệm về việc chìm/vượt qua đầu vào, vì các sự kiện không hỗ trợ hành vi này.Tuy nhiên, ContextActionService có hành vi này thông qua BindAction .

  • Trong khi một con chuột có thể không có sẵn trên tất cả các nền tảng, con chuột vẫn sẽ hoạt động trên di động (chạm) và bảng điều khiển (gamepad), điều này thường không có phần cứng chuột hoặc con trỏ.Đối với các hành vi cụ thể trên nhiều nền tảng, hãy sử dụng UserInputServiceContextActionService.

    Xem Nhập và máy ảnh để có thêm thông tin về tùy chỉnh nhập trong trải nghiệm của bạn.

Tóm Tắt

Thuộc Tính

  • Chỉ Đọc
    Không Sao Chép
    Đọc Song Song

    The CFrame vị trí của chuột trong không gian 3D.

  • Icon:ContentId
    Đọc Song Song

    ID nội dung của hình ảnh được sử dụng làm biểu tượng Mouse.

  • Chỉ Đọc
    Không Sao Chép
    Đọc Song Song

    Một CFrame được đặt tại Workspace.CurrentCamera và hướng về phía vị trí 3D của chuột.

  • Chỉ Đọc
    Không Sao Chép
    Đọc Song Song

    Vật thể trong không gian 3D mà mouse đang chỉ vào.

  • Đọc Song Song

    Xác định một đối tượng (và con cháu của nó) bị bỏ qua khi xác định Mouse.HitMouse.Target .

  • Chỉ Đọc
    Không Sao Chép
    Đọc Song Song

    Chỉ ra Enum.NormalId bề mặt BasePart mà con trỏ đang chỉ vào.

  • Chỉ Đọc
    Không Sao Chép
    Đọc Song Song

    Một Ray hướng tới vị trí thế giới của chuột, xuất phát từ vị trí thế giới Workspace.CurrentCamera.

  • Chỉ Đọc
    Không Sao Chép
    Đọc Song Song

    Mô tả chiều rộng của cửa sổ trò chơi bằng像素.

  • Chỉ Đọc
    Không Sao Chép
    Đọc Song Song

    Mô tả chiều cao của cửa sổ trò chơi bằng像素.

  • Chỉ Đọc
    Không Sao Chép
    Đọc Song Song

    Mô tả thành phần X (ngang) của vị trí chuột trên màn hình.

  • Chỉ Đọc
    Không Sao Chép
    Đọc Song Song

    Mô tả thành phần Y (ngang) của vị trí màn hình của chuột.

Sự Kiện

Thuộc Tính

Chỉ Đọc
Không Sao Chép
Đọc Song Song

Thuộc tính này chỉ ra CFrame vị trí của chuột trong không gian 3D. Lưu ý rằng Mouse.TargetFilter và con cháu của nó sẽ bị bỏ qua.

Các nhà phát triển có thể nhận được vị trí của Hit như vậy:


local position = mouse.Hit.Position

Hit thường được sử dụng bởi Tools để bắn vũ khí về phía chuột trong người thứ ba.

Các nhà phát triển tìm kiếm BasePart chuột đang chỉ vào nên sử dụng Mouse.Target .

Mouse.Hit được tính toán như thế nào?

Vị trí của Hit CFrame được tính là điểm giao nhau giữa chuột nội bộ Ray (một phiên bản mở rộng của Mouse.UnitRay ) và một đối tượng trong không gian 3D (như một phần).

Hướng của CFrame trúng khớp với hướng của Mouse.UnitRay .


local unitRayDirection = mouse.UnitRay.Direction
local mouseHitDirection = mouse.Hit.lookVector
-- unitRayDirection ≈ mouseHitDirection
-- the vectors are approximately equal

Lưu ý, cuộn của Workspace.CurrentCamera không được sử dụng khi tính toán hướng của Hit CFrame .

Tia nội bên của chuột mở rộng đến 1,000 đinh tán.Nếu con trỏ không chỉ vào một đối tượng trong không gian 3D ( ví dụ khi chỉ vào bầu trời ), thuộc tính này sẽ cách xa điểm Workspace.CurrentCamera 1,000 đinh tán.

Mẫu mã

The code in this sample, when placed inside a LocalScript within StarterPlayerScripts will draw a red laser beam between the character's head and Mouse.Hit at all times.

Note, this beam will pass directly through obstructions in third person as the Mouse's raycasting is done from the Workspace.CurrentCamera not the head.

Mouse.Hit Laser Beam

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local beam = Instance.new("Beam")
beam.Segments = 1
beam.Width0 = 0.2
beam.Width1 = 0.2
beam.Color = ColorSequence.new(Color3.new(1, 0, 0))
beam.FaceCamera = true
local attachment0 = Instance.new("Attachment")
local attachment1 = Instance.new("Attachment")
beam.Attachment0 = attachment0
beam.Attachment1 = attachment1
beam.Parent = workspace.Terrain
attachment0.Parent = workspace.Terrain
attachment1.Parent = workspace.Terrain
local function onRenderStep()
local character = player.Character
if not character then
beam.Enabled = false
return
end
local head = character:FindFirstChild("Head")
if not head then
beam.Enabled = false
return
end
beam.Enabled = true
local origin = head.Position
local finish = mouse.Hit.Position
attachment0.Position = origin
attachment1.Position = finish
end
RunService.RenderStepped:Connect(onRenderStep)

The code below visualizes the difference between Mouse.Hit and Mouse.Origin. In order to do this, the code uses the Vector3 positions of the hit and origin CFrame values using .p.

The difference is that the origin is "where the mouse came from" (its origin) and the hit is the position where the mouse hits (is when the player presses their mouse).

This example also visualizes that the mouse origin is very similar to the position of the CurrentCamera by printing the magnitude (distance) between the two positions.

Mouse Origin vs Mouse Hit vs CurrentCamera Position

local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local player = Players.LocalPlayer
local camera = Workspace.CurrentCamera
local mouse = player:GetMouse()
local camPos = camera.CFrame.Position
local function onButton1Down()
print("Mouse.Hit:", mouse.Hit.Position)
print("camPos:", camPos)
print("Mouse.Origin:", mouse.Origin.Position)
print("Magnitude:", (mouse.Origin.Position - camPos).Magnitude)
end
mouse.Button1Down:Connect(onButton1Down)

Icon

ContentId
Đọc Song Song

Biểu tượng là một thuộc tính xác định hình ảnh được sử dụng làm điểm chỉ.Nếu trống, một mũi tên mặc định được sử dụng.Trong khi con trỏ lượn qua một GuiButton, tính chất này được lược bỏ tạm thời.

Để ẩn hoàn toàn con trỏ, không sử dụng hình ảnh trong suốt - thay vào đó, hãy đặt UserInputService.MouseIconEnabled thành false.

Để nhận được/thiết lập biểu tượng chuột người dùng trong các trải nghiệm, bạn nên sử dụng UserInputService.MouseIcon .Mouse.Icon sẽ bị loại bỏ sau khi API mới cho plugin thiết lập con trỏ chuột được phát hành.

Thiết kế một cursor

Các hướng dẫn sau có thể chứng minh hữu ích khi tạo con trỏ chuột riêng của bạn:

  • Kích thước của hình ảnh được sử dụng xác định kích thước của con trỏ.
  • Trung tâm của hình ảnh là nơi phát ra lệnh nhập chuột.
  • Hình ảnh chuột mặc định là 64x64像, với chuột chiếm 17x24像 không gian.
Hệ thống Cursors

Khi sử dụng một PluginMouse được lấy từ Plugin:GetMouse() , bạn có thể sử dụng các biểu tượng sau đây tương tự như con trỏ mặc định của hệ thống, chẳng hạn như bàn tay, mũi tên, I-beam, v.v.Bạn có thể sử dụng chúng với các sự kiện GUI như MouseEnter, MouseLeaveMouseButton1Down để cung cấp một trải nghiệm Studio nhất quán khi tương tác với một số loại thành phần GUI nhất định.Lưu ý rằng chúng chỉ hoạt động cho plugin Studio; chúng sẽ không hoạt động cho các đối tượng khác Mouse .


<th>Tài sản</th>
<th>Sử dụng đề xuất</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<img src="../../../assets/misc/Mouse-Icon-Pointer.png" width="30">
</img>
</td>
<td><code>rbxasset://SystemCursors/Arrow</code> ></td><td>Nhấp chuột và lựa chọn mặc định.</td>
</tr>
<tr>
<td>
<img src="../../../assets/misc/Mouse-Icon-PointingHand.png" width="30">
</img>
</td>
<td><code>rbxasset://SystemCursors/PointingHand</code> ></td><td>Di chuột qua một liên kết/ nút hoạt động.</td>
</tr>
<tr>
<td>
<img src="../../../assets/misc/Mouse-Icon-OpenHand.png" width="30">
</img>
</td>
<td><code>rbxasset://SystemCursors/OpenHand</code> ></td><td>Di chuột qua một vật phẩm có thể kéo.</td>
</tr>
<tr>
<td>
<img src="../../../assets/misc/Mouse-Icon-GrabbingHand.png" width="30">
</img>
</td>
<td><code>rbxasset://SystemCursors/ClosedHand</code></td><td>Kéo một vật phẩm.</td>
</tr>
<tr>
<td>
<img src="../../../assets/misc/Mouse-Icon-IBeam.png" width="30">
</img>
</td>
<td><code>rbxasset://SystemCursors/IBeam</code></td><td>Lướt qua trong một lĩnh vực văn bản.</td>
</tr>
<tr>
<td>
<img src="../../../assets/misc/Mouse-Icon-ResizeNS.png" width="30">
</img>
</td>
<td><code>rbxasset://SystemCursors/SizeNS</code> ></td><td>Di chuột qua một tay cầm thay đổi kích thước ngang.</td>
</tr>
<tr>
<td>
<img src="../../../assets/misc/Mouse-Icon-ResizeEW.png" width="30">
</img>
</td>
<td><code>rbxasset://SystemCursors/SizeEW</code> ></td><td>Di chuột qua một tay cầm thay đổi kích thước ngang.</td>
</tr>
<tr>
<td>
<img src="../../../assets/misc/Mouse-Icon-ResizeNESW.png" width="30">
</img>
</td>
<td><code>rbxasset://SystemCursors/SizeNESW</code></td><td>Vị trí di chuột trên một tay cầm thay đổi kích thước góc.</td>
</tr>
<tr>
<td>
<img src="../../../assets/misc/Mouse-Icon-ResizeNWSE.png" width="30">
</img>
</td>
<td><code>rbxasset://SystemCursors/SizeNWSE</code> ></td><td>Di chuột qua tay cầm thay đổi kích thước góc.</td>
</tr>
<tr>
<td>
<img src="../../../assets/misc/Mouse-Icon-ResizeAll.png" width="30">
</img>
</td>
<td><code>rbxasset://SystemCursors/SizeAll</code> ></td><td>Di chuột qua một tay cầm thay đổi kích thước nhiều chiều.</td>
</tr>
<tr>
<td>
<img src="../../../assets/misc/Mouse-Icon-ResizeSplitV.png" width="30">
</img>
</td>
<td><code>rbxasset://SystemCursors/SplitNS</code></td><td>Chuyển qua một tay cầm "chia" dọc.</td>
</tr>
<tr>
<td>
<img src="../../../assets/misc/Mouse-Icon-ResizeSplitH.png" width="30">
</img>
</td>
<td><code>rbxasset://SystemCursors/SplitEW</code></td><td>Chuyển qua một tay cầm "chia" ngang.</td>
</tr>
<tr>
<td>
<img src="../../../assets/misc/Mouse-Icon-Forbidden.png" width="30">
</img>
</td>
<td><code>rbxasset://SystemCursors/Forbidden</code> ></td><td>Di chuột qua một vật phẩm bị khóa/bị cấm.</td>
</tr>
<tr>
<td>
<img src="../../../assets/misc/Mouse-Icon-Wait.png" width="30">
</img>
</td>
<td><code>rbxasset://SystemCursors/Wait</code></td><td>Chỉ ra rằng một hành động đang diễn ra.</td>
</tr>
<tr>
<td>
<img src="../../../assets/misc/Mouse-Icon-Busy.png" width="30">
</img>
</td>
<td><code>rbxasset://SystemCursors/Busy</code></td><td>Chỉ ra hệ thống đang bận rộn.</td>
</tr>
<tr>
<td>
<img src="../../../assets/misc/Mouse-Icon-Crosshair.png" width="30">
</img>
</td>
<td><code>rbxasset://SystemCursors/Cross</code></td><td>Chuyển qua khu vực lựa chọn chính xác.</td>
</tr>
</tbody>
Xem*
\* Những xuất hiện này là những ước lượng - cái nhìn thực sự phụ thuộc vào hệ điều hành của bạn.

Mẫu mã

This example changes the Players.LocalPlayer mouse icon to look like a dragon image.

Dragon Mouse Icon

local Players = game:GetService("Players")
local mouse = Players.LocalPlayer:GetMouse()
mouse.Icon = "http://www.roblox.com/asset?id=163023520"

Origin

Chỉ Đọc
Không Sao Chép
Đọc Song Song

Thuộc tính nguồn Mouse là một thuộc tính CFrame chỉ ra nơi con trỏ xuất phát.Nó được đặt tại Workspace.CurrentCamera và hướng về phía vị trí 3D của con chuột.

Mouse.UnitRay bắt đầu ở cùng vị trí với Nguồn, và mở rộng sang một đinh tán trong cùng một hướng.


local unitRay = mouse.UnitRay
local origin = mouse.Origin
-- unitRay.Direction = nguồn.p
-- unitRay.Direction ≈ origin.lookVector

Đối với vị trí của Mouse trong không gian 3D, xem Mouse.Hit .

Mẫu mã

The code below visualizes the difference between Mouse.Hit and Mouse.Origin. In order to do this, the code uses the Vector3 positions of the hit and origin CFrame values using .p.

The difference is that the origin is "where the mouse came from" (its origin) and the hit is the position where the mouse hits (is when the player presses their mouse).

This example also visualizes that the mouse origin is very similar to the position of the CurrentCamera by printing the magnitude (distance) between the two positions.

Mouse Origin vs Mouse Hit vs CurrentCamera Position

local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local player = Players.LocalPlayer
local camera = Workspace.CurrentCamera
local mouse = player:GetMouse()
local camPos = camera.CFrame.Position
local function onButton1Down()
print("Mouse.Hit:", mouse.Hit.Position)
print("camPos:", camPos)
print("Mouse.Origin:", mouse.Origin.Position)
print("Magnitude:", (mouse.Origin.Position - camPos).Magnitude)
end
mouse.Button1Down:Connect(onButton1Down)

Target

Chỉ Đọc
Không Sao Chép
Đọc Song Song

Vật thể trong không gian 3D mà mouse đang chỉ vào.

Ghi chú:

  • Nếu Mouse.TargetFilter đã được cài đặt, bộ lọc mục tiêu và con cháu của nó sẽ bị bỏ qua.
  • Khi con trỏ không chỉ vào một BasePart , ví dụ khi nó chỉ vào bầu trời, Mục tiêu sẽ là nil .
  • Các nhà phát triển tìm kiếm vị trí của chuột trong không gian 3D nên sử dụng Mouse.Hit .

Mẫu mã

The following code sample, when placed in StarterPlayerScripts will create a tool in the player's backpack that, once equipped, will change the BasePart.BrickColor of every BasePart the player clicks on.

Color Randomizer Tool

local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local backpack = localPlayer:WaitForChild("Backpack")
local tool = Instance.new("Tool")
tool.RequiresHandle = false
tool.CanBeDropped = false
tool.Parent = backpack
tool.Equipped:Connect(function(mouse)
mouse.Button1Down:Connect(function()
if mouse.Target and mouse.Target.Parent then
mouse.Target.BrickColor = BrickColor.random()
end
end)
end)

TargetFilter

Đọc Song Song

Thuộc tính này xác định một đối tượng bị bỏ qua bởi chuột khi tính toán Mouse.HitMouse.Target .Các con cháu của đối tượng cũng bị bỏ qua, vì vậy bạn có thể bỏ qua nhiều đối tượng miễn là chúng là con cháu của đối tượng mà thuộc tính này được cài đặt.Tính năng này hữu ích khi lọc các mô hình có chứa hiệu ứng đặc biệt hoặc trang trí không nên ảnh hưởng đến Mouse.Hit hoặc Mouse.Target .

Thuộc tính này có thể được đặt thành bất kỳ Instance hoặc nil , ví dụ:


local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
mouse.TargetFilter = Workspace.Model

Lưu ý rằng Character của Players.LocalPlayer bị bỏ qua bởi chuột tự động.

TargetSurface

Chỉ Đọc
Không Sao Chép
Đọc Song Song

Thuộc tính này chỉ ra Enum.NormalId bề mặt BasePart mà con trỏ đang chỉ vào.Thuộc tính này được xác định từ vị trí thế giới của chuột ( Mouse.Hit ) và phần mà chuột đang chỉ vào ( Mouse.Target ).

Thuộc tính này không có ý nghĩa khi con trỏ không chỉ vào một phần, ví dụ khi con trỏ chỉ vào bầu trời.Tại thời điểm này, thuộc tính này được đặt thành 'Phải' trong những trường hợp này.Trước khi sử dụng tính năng này, hãy kiểm tra xem mục tiêu của chuột không phải là nil .


local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
-- Kiểm tra có tồn tại một phần mà con trỏ đang chỉ vào không
if mouse.Target then
print("The mouse is pointing to the " .. mouse.TargetSurface.Name .. " side of " .. mouse.Target.Name)
else
print("The mouse is not pointing at anything.")
end

Mẫu mã

The code in this sample, when placed in a LocalScript inside StarterPlayerScripts will set the surface of any BasePart clicked on to a random surface.

Surface Randomizer

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local surfaceTypes = {
Enum.SurfaceType.Smooth,
Enum.SurfaceType.Glue,
Enum.SurfaceType.Weld,
Enum.SurfaceType.Studs,
Enum.SurfaceType.Inlet,
Enum.SurfaceType.Universal,
Enum.SurfaceType.Hinge,
Enum.SurfaceType.Motor,
}
local function onMouseClick()
-- make sure the mouse is pointing at a part
local target = mouse.Target
if not target then
return
end
local surfaceType = surfaceTypes[math.random(1, #surfaceTypes)]
local surface = mouse.TargetSurface
local propertyName = surface.Name .. "Surface"
mouse.Target[propertyName] = surfaceType
end
mouse.Button1Down:Connect(onMouseClick)

UnitRay

Chỉ Đọc
Không Sao Chép
Đọc Song Song

Thuộc tính UnitRay là một Ray hướng tới vị trí của chuột trong không gian 3D (được mô tả bởi Mouse.Hit ).Nó xuất phát từ CFrame của Workspace.CurrentCamera.Giống như tất cả các tia đơn vị, nó có khoảng cách 1.


local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
print(mouse.UnitRay.Direction.Magnitude) -- Always 1

ViewSizeX

Chỉ Đọc
Không Sao Chép
Đọc Song Song

Thuộc tính ViewSizeX mô tả thành phần ngang của kích thước cửa sổ trò chơi bằng像素.

Mẫu mã

This code sample shows how you can create a Vector2 representing the Mouse object's position on screen (X() and Y()) and the size of the screen itself (ViewSizeX() and ViewSizeY()). Using these, you can normalize the position of the mouse on-screen such that the top-left just under the topbar maps to (0, 0) and the bottom-right maps to (1, 1). This normalized position is calculated and printed as the mouse moves using the Move() event.

Normalized Mouse Position

local Players = game:GetService("Players")
-- Note: You should use ContextActionService or UserInputService instead of
-- the Mouse object for accomplishing this task.
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local function onMouseMove()
-- Construct Vector2 objects for the mouse's position and screen size
local position = Vector2.new(mouse.X, mouse.Y)
local size = Vector2.new(mouse.ViewSizeX, mouse.ViewSizeY)
-- A normalized position will map the top left (just under the topbar)
-- to (0, 0) the bottom right to (1, 1), and the center to (0.5, 0.5).
-- This is calculated by dividing the position by the total size.
local normalizedPosition = position / size
print(normalizedPosition)
end
mouse.Move:Connect(onMouseMove)

ViewSizeY

Chỉ Đọc
Không Sao Chép
Đọc Song Song

Thuộc tính ViewSizeY mô tả thành phần ngang của kích thước cửa sổ trò chơi bằng pixel. Chiều dài này bao gồm không gian được sử dụng bởi thanh trên cùng.

Mẫu mã

This code sample shows how you can create a Vector2 representing the Mouse object's position on screen (X() and Y()) and the size of the screen itself (ViewSizeX() and ViewSizeY()). Using these, you can normalize the position of the mouse on-screen such that the top-left just under the topbar maps to (0, 0) and the bottom-right maps to (1, 1). This normalized position is calculated and printed as the mouse moves using the Move() event.

Normalized Mouse Position

local Players = game:GetService("Players")
-- Note: You should use ContextActionService or UserInputService instead of
-- the Mouse object for accomplishing this task.
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local function onMouseMove()
-- Construct Vector2 objects for the mouse's position and screen size
local position = Vector2.new(mouse.X, mouse.Y)
local size = Vector2.new(mouse.ViewSizeX, mouse.ViewSizeY)
-- A normalized position will map the top left (just under the topbar)
-- to (0, 0) the bottom right to (1, 1), and the center to (0.5, 0.5).
-- This is calculated by dividing the position by the total size.
local normalizedPosition = position / size
print(normalizedPosition)
end
mouse.Move:Connect(onMouseMove)
Chỉ Đọc
Không Sao Chép
Đọc Song Song

Khi phát hiện các thay đổi vị trí của chuột trên màn hình, được khuyến khích bạn sử dụng ContextActionService:BindAction() với Enum.UserInputType.MouseMovement hoặc UserInputService.InputChanged, mà cả hai đều mô tả vị trí của chuột bằng Position (một Vector3) của một InputObject, thay vì sử dụng các thuộc tính này và liên quan.

Thuộc tính X mô tả thành phần ngang của vị trí chuột trên màn hình.Vị trí được đo bằng pixel so với góc trên cùng bên trái, dưới thanh trên cùng.Tính năng này có thể được sử dụng kết hợp với Mouse.Y để sản xuất Vector2 đại diện cho vị trí của con trỏ:


local position = Vector2.new(mouse.X, mouse.Y)

Thuộc tính này không bắn Changed hoặc tín hiệu được trả về từ GetPropertyChangedSignal . Sử dụng sự kiện Mouse.Move thay thế.

Mẫu mã

This code sample shows how you can create a Vector2 representing the Mouse object's position on screen (X() and Y()) and the size of the screen itself (ViewSizeX() and ViewSizeY()). Using these, you can normalize the position of the mouse on-screen such that the top-left just under the topbar maps to (0, 0) and the bottom-right maps to (1, 1). This normalized position is calculated and printed as the mouse moves using the Move() event.

Normalized Mouse Position

local Players = game:GetService("Players")
-- Note: You should use ContextActionService or UserInputService instead of
-- the Mouse object for accomplishing this task.
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local function onMouseMove()
-- Construct Vector2 objects for the mouse's position and screen size
local position = Vector2.new(mouse.X, mouse.Y)
local size = Vector2.new(mouse.ViewSizeX, mouse.ViewSizeY)
-- A normalized position will map the top left (just under the topbar)
-- to (0, 0) the bottom right to (1, 1), and the center to (0.5, 0.5).
-- This is calculated by dividing the position by the total size.
local normalizedPosition = position / size
print(normalizedPosition)
end
mouse.Move:Connect(onMouseMove)
Chỉ Đọc
Không Sao Chép
Đọc Song Song

Khi phát hiện các thay đổi vị trí của chuột trên màn hình, được khuyến khích bạn sử dụng ContextActionService:BindAction() với Enum.UserInputType.MouseMovement hoặc UserInputService.InputChanged, mà cả hai đều mô tả vị trí của chuột bằng Position (một Vector3) của một InputObject, thay vì sử dụng các thuộc tính này và liên quan.

Thuộc tính Y mô tả thành phần ngang của vị trí chuột trên màn hình.Vị trí được đo bằng pixel so với góc trên cùng bên trái, dưới thanh trên cùng.Tính năng này có thể được sử dụng kết hợp với Mouse.X để sản xuất Vector2 đại diện cho vị trí của con trỏ:


local position = Vector2.new(mouse.X, mouse.Y)

Thuộc tính này không bắn Changed hoặc tín hiệu được trả về từ GetPropertyChangedSignal . Sử dụng sự kiện Mouse.Move thay thế.

Mẫu mã

This code sample shows how you can create a Vector2 representing the Mouse object's position on screen (X() and Y()) and the size of the screen itself (ViewSizeX() and ViewSizeY()). Using these, you can normalize the position of the mouse on-screen such that the top-left just under the topbar maps to (0, 0) and the bottom-right maps to (1, 1). This normalized position is calculated and printed as the mouse moves using the Move() event.

Normalized Mouse Position

local Players = game:GetService("Players")
-- Note: You should use ContextActionService or UserInputService instead of
-- the Mouse object for accomplishing this task.
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local function onMouseMove()
-- Construct Vector2 objects for the mouse's position and screen size
local position = Vector2.new(mouse.X, mouse.Y)
local size = Vector2.new(mouse.ViewSizeX, mouse.ViewSizeY)
-- A normalized position will map the top left (just under the topbar)
-- to (0, 0) the bottom right to (1, 1), and the center to (0.5, 0.5).
-- This is calculated by dividing the position by the total size.
local normalizedPosition = position / size
print(normalizedPosition)
end
mouse.Move:Connect(onMouseMove)

Phương Pháp

Sự Kiện

Button1Down

Sự kiện này xảy ra khi người chơi nhấn nút chuột trái của họ.Lưu ý rằng điều này có thể truy cập từ một Tool ; ví dụ, khi được đặt trong một LocalScript , mã bên dưới sẽ in ra Button1Down mỗi khi nhấn nút chuột bên trái.


local tool = script.Parent -- Hãy chắc chắn rằng đây là một đối tượng Công cụ
tool.Equipped:Connect(function(mouse)
mouse.Button1Down:Connect(function()
print("Button1Down")
end)
end)

Bạn có thể tìm ra vị trí của con trỏ trong không gian thế giới và nếu nó chỉ vào bất kỳ BasePart hoặc Target tính chất nào bằng cách sử dụng Hit và tính chất.


Button1Up

Sự kiện này bắt lửa khi người chơi phát hành nút chuột trái của họ.Lưu ý rằng điều này có thể truy cập từ một Tool ; ví dụ, khi được đặt trong một LocalScript , mã bên dưới sẽ in ra Button1Up mỗi khi nút chuột trái được phát hành.


local tool = script.Parent -- Hãy chắc chắn rằng đây là một đối tượng Công cụ
tool.Equipped:Connect(function(mouse)
mouse.Button1Up:Connect(function()
print("Button1Up")
end)
end)

Bạn có thể tìm ra vị trí của con trỏ trong không gian thế giới và nếu nó chỉ vào bất kỳ BasePart hoặc Target tính chất nào bằng cách sử dụng Hit và tính chất.


Button2Down

Sự kiện này xảy ra khi người chơi nhấn nút chuột phải của họ.Lưu ý rằng điều này có thể truy cập từ một Tool ; ví dụ, khi được đặt trong một LocalScript , mã bên dưới sẽ in ra Button2Down mỗi khi nhấn nút chuột phải.


local tool = script.Parent -- Hãy chắc chắn rằng đây là một đối tượng Công cụ
tool.Equipped:Connect(function(mouse)
mouse.Button2Down:Connect(function()
print("Button2Down")
end)
end)

Bạn có thể tìm ra vị trí của con trỏ trong không gian thế giới và nếu nó chỉ vào bất kỳ BasePart hoặc Target tính chất nào bằng cách sử dụng Hit và tính chất.


Button2Up

Sự kiện này bắt lửa khi người chơi phát hành nút chuột phải của họ.Lưu ý rằng điều này có thể truy cập từ một Tool ; ví dụ, khi được đặt trong một LocalScript , mã bên dưới sẽ in ra Button2Up mỗi khi nút chuột phải được phát hành.


local tool = script.Parent -- Hãy chắc chắn rằng đây là một đối tượng Công cụ
tool.Equipped:Connect(function(mouse)
mouse.Button2Up:Connect(function()
print("Button2Up")
end)
end)

Bạn có thể tìm ra vị trí của con trỏ trong không gian thế giới và nếu nó chỉ vào bất kỳ BasePart hoặc Target tính chất nào bằng cách sử dụng Hit và tính chất.


Idle

Bị sa thả trong mỗi nhịp tim mà chuột không được chuyển đến sự kiện chuột khác.

Lưu ý, sự kiện này không nên được sử dụng để xác định khi nào con trỏ vẫn còn. Khi nó bắn mỗi nhịp tim, nó sẽ bắn giữa các sự kiện Mouse.Move.

Đối với thông tin về cách nhận được đối tượng Mouse, vui lòng xem trang Mouse.

Các nhà phát triển có thể tìm ra vị trí của con trỏ trong không gian thế giới, và nếu nó chỉ vào bất kỳ BasePart nào sử dụng các thuộc tính Mouse.HitMouse.Target .

Lưu ý, các nhà phát triển được khuyến khích sử dụng UserInputService thay vì đối tượng Mouse trong công việc mới.


Mẫu mã

This example demonstrates how mouse events are passed during each frame

Mouse.Idle

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local events = {
"Button1Down",
"Button1Up",
"Button2Down",
"Button2Up",
"Idle",
"Move",
"WheelBackward",
"WheelForward",
"KeyDown",
"KeyUp",
}
local currentEvent
local frame = 0
local function processInput()
frame = frame + 1
print("Frame", frame, "- mouse event was passed to", currentEvent)
end
for _, event in pairs(events) do
mouse[event]:Connect(function()
currentEvent = event
end)
end
RunService:BindToRenderStep("ProcessInput", Enum.RenderPriority.Input.Value, processInput)

Move

Bị sa thả khi chuột được di chuyển.

Lưu ý, sự kiện này được kích hoạt khi vị trí của con chuộtđược cập nhật, do đó nó sẽ bắn lặp lại trong khi di chuyển.

Đối với thông tin về cách nhận được đối tượng Mouse, vui lòng xem trang Mouse.

Các nhà phát triển có thể tìm ra vị trí của con trỏ trong không gian thế giới, và nếu nó chỉ vào bất kỳ BasePart nào sử dụng các thuộc tính Mouse.HitMouse.Target .


mouse.Move:Connect(function()
local position = mouse.Hit.p
local target = mouse.Target
print(target, position)
end)

Lưu ý, các nhà phát triển được khuyến khích sử dụng UserInputService thay vì đối tượng Mouse trong công việc mới.


Mẫu mã

The example below allows the local player to move parts with their mouse.

When the player presses their left mouse button over a part, that part is the mouse's target and becomes the point. Until the player releases their left mouse button, that part will move to the mouse's world position when the player moves their mouse.

Note that the Mouse.TargetFilter property allows the code to ignore the part being moved when determining the mouse's world position.

The code should work as expected when placed in a LocalScript.

Move Parts with the Mouse

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local point
local down
local function selectPart()
if mouse.Target and not mouse.Target.Locked then
point = mouse.Target
mouse.TargetFilter = point
down = true
end
end
local function movePart()
if down and point then
local posX, posY, posZ = mouse.Hit.X, mouse.Hit.Y, mouse.Hit.Z
point.Position = Vector3.new(posX, posY, posZ)
end
end
local function deselectPart()
down = false
point = nil
mouse.TargetFilter = nil
end
mouse.Button1Down:Connect(selectPart)
mouse.Button1Up:Connect(deselectPart)
mouse.Move:Connect(movePart)

WheelBackward

Sự kiện WheelBackward bắt lửa khi bánh xe chuột được cuộn lùi.Các trường hợp sử dụng có thể cho sự kiện này bao gồm chuyển đổi phạm vi súng trong một kẻ bắn súng thứ nhất (FPS) hoặc thu phóng camera của người chơi.

Điều này có thể được sử dụng cùng với sự kiện cuộn trước, Mouse.WheelForward .

Đối với thông tin về cách nhận được đối tượng Mouse, vui lòng xem trang Mouse.

Lưu ý, các nhà phát triển được khuyến khích sử dụng UserInputService thay vì đối tượng Mouse trong công việc mới.


Mẫu mã

The below example assumes that you have already got the player's mouse (and set it as a variable named 'mouse'), whether by use of a Tool, HopperBin or the Player:GetMouse() method. It will print "Wheel went backwards!" when the player scrolls backwards.

Mouse.WheelBackward

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local function onWheelBackward()
print("Wheel went backwards!")
end
mouse.WheelBackward:Connect(onWheelBackward)

WheelForward

Sự kiện WheelForward bắt lửa khi bánh xe chuột được cuộn về phía trước.Các trường hợp sử dụng có thể cho sự kiện này bao gồm chuyển đổi phạm vi súng trong một kẻ bắn súng thứ nhất (FPS) hoặc thu phóng camera của người chơi.

Điều này có thể được sử dụng cùng với sự kiện cuộn lùi, Mouse.WheelBackward .

Đối với thông tin về cách nhận được đối tượng Mouse, vui lòng xem trang Mouse.

Lưu ý, các nhà phát triển được khuyến khích sử dụng UserInputService thay vì đối tượng Mouse trong công việc mới.


Mẫu mã

The below example assumes that you have already got the player's mouse (and set it as a variable named 'mouse'), whether by use of a Tool, HopperBin or the Player:GetMouse() method. It will print "Wheel went forward!" when the player scrolls forwards.

Mouse.WheelForward

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local function onWheelBackward()
print("Wheel went forward!")
end
mouse.WheelForward:Connect(onWheelBackward)