I am trying to replicate this spell casting system from the Roblox game A:FR testing - Roblox. It is based around pressing a button to enter a state (in the game, it’s the C key) and then you are able to click and hold the right mouse button to drag the mouse in different directions to activate one of four squares on a screen in a sequence that results in an action.
The pattern part isn’t my problem though. It’s the math and logic behind the mouse part.
So far this is what I have (not the full script):
local is_Casting = false
local MouseButton2Pressed = false
local lastMouseX, lastMouseY = Mouse.X, Mouse.Y
local last_tick = os.clock()
local direction
function onMouseMove()
MouseButton2Pressed = UIS:IsMouseButtonPressed(Enum.UserInputType.MouseButton2)
local viewportSize = Camera.ViewportSize
local dt = os.clock() - last_tick
local d2x = (Mouse.X - lastMouseX)/dt
local d2y = (Mouse.Y - lastMouseY)/dt
local angle_of_mouse_direction = math.floor(math.deg(math.atan(d2y/d2x)))
local mouse_magnitude = math.floor(math.sqrt(d2x^2 + d2y^2))
if MouseButton2Pressed and is_Casting then
--print(mouse_magnitude , angle_of_mouse_direction)
if mouse_magnitude >= 450 then
if (math.abs(d2x) > math.abs(d2y)) then --[moving more horizontally than vertically]
if d2x < 0 and not (direction == "left") then
direction = "left"
print(direction, angle_of_mouse_direction)
elseif d2x >= 0 and not (direction == "right") then
direction = "right"
print(direction, angle_of_mouse_direction)
end
else
if d2y < 0 and not (direction == "up") then
direction = "up"
print(direction, angle_of_mouse_direction)
elseif d2y >= 0 and not (direction == "down") then
direction = "down"
print(direction, angle_of_mouse_direction)
end
end
end
end
lastMouseX, lastMouseY = Mouse.X, Mouse.Y
last_tick = os.clock()
end
In my attempt to replicate, you have to move your mouse at a certain speed. When you do that, the script then checks if you’re moving faster horizontally or faster vertically. I just took the change in position of the mouse’s x and y over time. And then see if it’s negative or positive. This is how I detected left and right, and up and down.
My problem is that, in my version, you have to move the mouse at an angle of at least 45 degrees or more in order to go up and down. Which makes it feel very unresponsive and not intuitive AT ALL, which is not what the game I linked above does. Like, if you were to move left then move diagonally to go up, you’d go right instead because d2x > d2y. The problem is diagonal movements are not having the desired effects
I tried accounting for the angle of the mouse movement, checking that if it’s at least >= 15 then go top or if the angle is <= -15 then go down, else go to the right if you’re at left, or go to left if you’re at right. But I legit could not get the logic down. I just ended up deleting that part because it didn’t work. Help from someone who is more knowledgeable than I am (basically anyone else but me) on this would be appreciated very much.
Here is a repro: Just press and hold E and 4 squares will show up. Then drag while holding right click.
MouseMovementTest.rbxl (40.4 KB)