I am attempting to create a sword which you can fight other players with. I wish to make this sword play different animations relevant to the players mouse position on the screen, so if the mouse was on the left side it could be a left swing or left block. What would be the best way to detect which sidebar of the screen the player says mouse is and how to play an animation when they click on that side. I’m open to any ideas, thank you.
I have a few ideas:
-
Get Mouse’s position on-screen and see which half of the screen it is on. If the mouse’s X position is greater than or equal to
Camera. ViewportSize. X/2
, it is on the right, otherwise it’s on the left -
Get mouse’s position relative to the center of the screen, so like:
local pos = Vector2.new(mouse.x, mouse.y)
local center = camera.ViewportSize / 2
local r = (pos - center)
if r.x < 0 then
-- left side
elseif r.x > 0 then
-- right side
end
- Get Mouse’s hit position relative to the HumanoidRootPart’s position, so like:
local r = HumanoidRootPart.CFrame:PointToObjectSpace(mouse.Hit.p)
One way you could do this is by creating invisible GUI buttons, covering the area of the screen that activates a specific animation.
Another way, is just to get the X/Y position of the mouse, and set up a few bits of logic to check if the mouse is in a specific region of the screen.
I think the former would be easier to do and you would have a bit more control of the activation zones.
This looks very promising, I will try this, thank you!
I have successfully performed this task, now I ask how I modify the 2 section screen detector to split the screen up to 4 different sections?
You’d also have to take the y-coordinate into account as well:
if r.x < 0 and r.y < 0 then
-- top left
elseif r.x < 0 and r.y > 0 then
-- bottom left
elseif r.x > 0 and r.y < 0 then
-- top right
elseif r.x > 0 and r.y > 0 then
-- bottom right
end
Perfect, thank you very much. I am excited for the results
Yeah this is pretty much already answered, but get the screen size and divide it into how many sections you want then just detect mouse position within the bounds of those sections.
How would it go about when it’s in first person?
In fact, you can use the InputChanged Event of UserInputService. The input object that’s being returned has a property containing the position. You’ll have to play around with MouseBehavior if you want to do it in first person.
how can I get the camera? Should I do Workspace.CurrentCamera or?