Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make a cursor get bigger the closer an object is in front of you?

I have no idea how to do this. If anyone'd like to help me, please do so. Essentially, if an object is close to you, and you're focused on it, the cursor becomes larger. If it's farther away, it gets smaller. Sorta like in this video: https://www.youtube.com/watch?v=4YTo4N-Vuo8


local Tool = script.Parent;

enabled = true
function onButton1Down(mouse)
	if not enabled then
		return
	end

	enabled = false
	mouse.Icon = "http://www.roblox.com/asset/?id=157167003"

	wait(7)
	mouse.Icon = "http://www.roblox.com/asset/?id=157167003"
	enabled = true

end

function onEquippedLocal(mouse)

	if mouse == nil then
		print("Mouse not found")
		return 
	end

	mouse.Icon = "http://www.roblox.com/asset/?id=157167003"
	mouse.Button1Down:connect(function() onButton1Down(mouse) end)
end


Tool.Equipped:connect(onEquippedLocal)

1 answer

There is unfortunately not an elegant way to do this using the mouse Icon property.


Resizing Mouse Icon

There are two options for achieving this effect:

  1. Use Mouse Icon, and upload a number of different icons with different amounts of empty space around the central sprite (but I believe same overall widths are necessary). More empty space --> smaller icon. Uploading as many as you feel is necessary to be a good continuum of sizes.

  2. Set the Mouse icon to something blank, and actually use some GUI object as the icon.


To go about implementing this, there are three components to consider:

a) Determining size of cursor (under either 1) or 2) from above) based on distance

b) Determining distance to thing pointing at

c) Updating the icon frequently


Determining Cursor Size for Distance

I will be considering this in terms of the GUI solution, but the math here could be used to switch between the equivalently sized icons.

We know several things about our width as a function of distance:

width(d) is maximally some number, and the maximum is at d = 0; width(d) is minimally some number, and the minimum is at d = ?. Here's a sample width function that you could use, but you could also use your own:

function cursorWidth(distance)
	return 20 + 20 / (distance/10 + 1);
end
--[[
Sample table of values:

D	W
----------
0	40
10	33.33
20	30
30	28
40	26.66
50	25.71
60	25
70	24.44
80	24
]]

Determining Distance to Thing Pointing At

This is an easy thing. It's the distance between mouse.Hit.p and the player's head (or camera if you would prefer)

local head = game.Players.LocalPlayer.Character.Head.Position;
local point = mouse.Hit.p;
local distance = (head - point).magnitude; -- In studs

Updating Cursor Constantly

So, obviously we need to constantly set the width of our icon in order for this work to be used. We could either do a while loop constantly working, or use the Moved event of the mouse.

This discussion is preserved as it was. New answers, comments, and votes are closed.