Directional Cursor

I am trying to make a system that changes the mouse rotation (the mouse is substituted by a image) based on it’s location, featuring north, south, west, east, northeast, southeast, etc.
The problem is that north, south, west and east don’t work, only the other ones somehow.
Here’s the script:

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = script.Parent.Cursor

local screenCenterX = mouse.Parent.AbsoluteSize.X /2
local screenCenterY = mouse.Parent.AbsoluteSize.Y / 2

local function updateCursorTexture()
    local mouseX, mouseY = mouse.Position.X.Offset, mouse.Position.Y.Offset
	local rotation = mouse.Rotation

    if mouseX < screenCenterX and mouseY < screenCenterY then
        rotation = -45
    elseif mouseX > screenCenterX and mouseY < screenCenterY then
        rotation = 45
    elseif mouseX < screenCenterX and mouseY > screenCenterY then
        rotation = -135
    elseif mouseX > screenCenterX and mouseY > screenCenterY then
        rotation = 135
	elseif mouseX < screenCenterX and mouseY == screenCenterY then
        rotation = -90
	elseif mouseX > screenCenterX and mouseY == screenCenterY then
		rotation = 90
	elseif mouseY < screenCenterY and mouseY == screenCenterY then
		rotation = 0
	elseif mouseY > screenCenterY and mouseY == screenCenterY then
		rotation = 180
    end

    mouse.Rotation = rotation
end

game:GetService("RunService").RenderStepped:Connect(updateCursorTexture)

Your diagonal checks override the cardinal ones, and rotation isn’t applied.

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = script.Parent.Cursor

local screenCenterX = mouse.Parent.AbsoluteSize.X / 2
local screenCenterY = mouse.Parent.AbsoluteSize.Y / 2

local function updateCursorTexture()
    local mouseX, mouseY = mouse.Position.X.Offset, mouse.Position.Y.Offset

    if mouseX < screenCenterX and mouseY == screenCenterY then
        mouse.Rotation = -90
    elseif mouseX > screenCenterX and mouseY == screenCenterY then
        mouse.Rotation = 90
    elseif mouseY < screenCenterY and mouseX == screenCenterX then
        mouse.Rotation = 0
    elseif mouseY > screenCenterY and mouseX == screenCenterX then
        mouse.Rotation = 180
    elseif mouseX < screenCenterX and mouseY < screenCenterY then
        mouse.Rotation = -45
    elseif mouseX > screenCenterX and mouseY < screenCenterY then
        mouse.Rotation = 45
    elseif mouseX < screenCenterX and mouseY > screenCenterY then
        mouse.Rotation = -135
    elseif mouseX > screenCenterX and mouseY > screenCenterY then
        mouse.Rotation = 135
    end
end

game:GetService("RunService").RenderStepped:Connect(updateCursorTexture)

If there any mistakes please excuse me am a new developer

I editted the script and forgot to add to the post, but at the end i added this:

    mouse.Rotation = rotation

The script you provided works perfectly, but the now working cardinals have little to no space to appear, but maybe adding a task.wait before changing the rotation will solve it, thanks!

1 Like

Why not get the angle at which the mouse is from the center and then formatting it accordingly?

local difference = screenCenter-mousePosition
local mouseAngle = math.atan2(difference.Y, difference.X)

Then you can just round the angle to the nearest 45th.

local rotation = math.round(math.deg(mouseAngle) / 45) * 45

How would that be applied? I’ve tried here but it doesn’t seem to work

Oh suprised that it worked thank

Something like:

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Cursor = script.Parent
local Camera = workspace.CurrentCamera

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

UserInputService.MouseIconEnabled = false

local function UpdateCursorTexture(deltaTime)
	local screenCenter = Camera.ViewportSize/2
	local mousePosition = UserInputService:GetMouseLocation()
	
	local difference = screenCenter-mousePosition
	local mouseAngle = math.atan2(difference.Y, difference.X)
	local rotation = math.round(math.deg(mouseAngle) / 45) * 45 - 45
	
	Cursor.Position = UDim2.fromOffset(mousePosition.X,mousePosition.Y)
	
	--print(rotation)
	
	Cursor.Rotation = rotation
end

RunService.RenderStepped:Connect(UpdateCursorTexture)

Would give you this result:

I tried replicating your (assumed) setup and I think it mimics nicely.

2 Likes

Yes, this is absolutely what i was looking for, thanks!

No worries. Good luck developing and have a good day.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.