PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Mouse Icon Appearance

Mouse Icon Appearance

Oct 27 2018, 8:49 AM PST 5 min

This tutorial explains how the appearance of the mouse can be customized. The examples given in this tutorial must be executed in LocalScripts to work.

Set mouse icon

Changing the icon of a player’s mouse is simply done in a LocalScript. The icon has to be a Decal and can be changed as often as you like.

local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Icon = "rbxassetid://163023520"

Note that since the icon is set in a LocalScript each player could have a different icon, or can have an icon appear in certain situations (such as when an ability is recharging or the mouse is hovering over a hostile target).

Hide mouse icon

The mouse icon’s visibility can be toggled on and off with UserInputService/MouseIconEnabled. This property must be changed from a LocalScript.

For example, the following code, when put in StarterGui, will switch the mouse from visible to invisible and back every two seconds:

local UserInputService = game:GetService("UserInputService")

while true do
	wait(2)
	UserInputService.MouseIconEnabled = false
	wait(2)
	UserInputService.MouseIconEnabled = true
end

Lock mouse position

The mouse position can be locked to the center of the screen using UserInputService/MouseBehavior. Setting this property to LockCenter will lock the mouse to the center of the screen, while Default will unlock the mouse.

If the mouse is locked, UserInputService/InputChanged will still fire when the player moves the mouse and will pass in the distance the mouse has moved.

local UserInputService = game:GetService("UserInputService")

UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserInputService.InputChanged:connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
		print("delta is (" .. tostring(inputObject.Delta.x) .. ", " ..  tostring(inputObject.Delta.y) .. ")")
	end
end)

Example

Using UserInputService/MouseIconEnabled and UserInputService/MouseBehavior allows for creating a custom camera system that moves the camera simply when the player moves their mouse left and right:

-- Lock mouse to center of the screen and hide it
local UserInputService = game:GetService("UserInputService")

UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserInputService.MouseIconEnabled = false

-- Change the player's camera to scriptable so it can be moved with code
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable

-- Bind function to when the mouse moves. If it moves left or right, adjust an angle variable which will be used later
local angle = 0
UserInputService.InputChanged:connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
		if inputObject.Delta.x < 0 then
			angle = angle - 5
		elseif inputObject.Delta.x > 0 then
			angle = angle + 5
		end
	end
end)

-- On renderstepped move the camera around the player based on the angle variable declared above
game:GetService("RunService").RenderStepped:connect(function()
	local x = 10 * math.cos(math.rad(angle))
	local z = 10 * math.sin(math.rad(angle))
	local camPosition = Vector3.new(x, 5, z)
	local torsoPosition = game.Players.LocalPlayer.Character.Torso.Position
	camPosition = camPosition + torsoPosition
	camera.CoordinateFrame = CFrame.new(camPosition, torsoPosition)
end)
Tags:
  • input
  • ui
  • gameplay
  • coding