So I was wondering how I can lock the mouse in the center of the screen, even if the user isnt in first person. How can I do this?
Before making a topic please learn how to use the search feature…
Sorry I forgot about that, Ill remove this topic
You’ll have to get a mod to do that at this point so you kinda have no choice but to mark their post as the solution
Im well aware, I can just do what I always do, flag the post and ask for them to remove it so its fine
Wait once the mouse is locked to the center, how can I tell when they move it? I mean using mouse.Move works but after that how can I tell which direction they are moving it in? Like up,Left,Down,etc?
The Mouse.Move event doesn’t return the mouse’s X and Y positions and so you’ll have to manually check.
Like you can have two variables like so:
local previousX, previousY = Mouse.X, Mouse.Y
These will hold the previous mouse positions.
Now when the mouse moves, we’re just gonna subtract the current position of the mouse by the one we saved
Mouse.Move:Connect(function()
-- we’re utilizing math.abs because we don’t want negative numbers
--[[ if the absolute value of the mouse’s
current X position subtracted
by the mouse’s previous X
position is less than the
absolute value of the mouse’s
current Y position subtracted
by the mouse’s previous Y
position then it’s non-eligible
]]
if math.abs(Mouse.X - previousX) < math.abs(Mouse.Y - previousY) then return end
local Direction = Vector2.new(Mouse.X, Mouse.Y) - Vector2.new(previousX, previousY)
-- now it’s time to check the direction
if Mouse.X - previousX > 0 then
print("Right")
else
print("Left")
end
if Mouse.Y - previousY > 0 then
print("Down")
else
print("Up")
end
previousX, previousY = Mouse.X, Mouse.Y
end)
Cited the math stuff (x - y > 0) from this post (Forummer is pretty cool):
All that this prints is up and left, it is a good step in the right direction
… guess not … still looking at it
Use UserInputService:GetMouseDelta()
, which returns the distance the mouse traveled since the last frame.
-- NOTE: I might have these the wrong way around, not sure off the top of my head; might be < 0 = left and > 0 = right, though you'll just have to test it to verify.
local mouseDelta = UserInputService:GetMouseDelta()
if mouseDelta.X > 0 then
print("Right")
else
print("Left")
end
if mouseDelta.Y > 0 then
print("Up")
else
print("Down")
end
To force the mouse to stay locked in the center of the screen, you have to set UserInputService.MouseBehavior
to Enum.MouseBehavior.LockCenter
:
local UserInputService = game:GetService("UserInputService")
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
You can find more information on these methods and properties in UserInputService’s documentation:
Broke it up into two working scripts
Problem is they don’t work together …
LockView
wait()
local UserInputService = game:GetService("UserInputService")
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserInputService.MouseIconEnabled = false
Movement
local UserInputService = game:GetService("UserInputService")
local lastMousePosition = UserInputService:GetMouseLocation()
local function mousemove(delta)
local direction
if math.abs(delta.x) > math.abs(delta.y) then
direction = (delta.x > 0) and "Right" or "Left"
else
direction = (delta.y > 0) and "Up" or "Down"
end
print("Mouse moved:", direction)
end
UserInputService.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement then
local currentMousePosition = UserInputService:GetMouseLocation()
local delta = Vector2.new(currentMousePosition.x - lastMousePosition.x, currentMousePosition.y - lastMousePosition.y)
if delta.magnitude > 0 then
mousemove(delta)
lastMousePosition = currentMousePosition
end
end
end)
may have to figure actual camera movement … angle vs centered for direction
This works! Except the Up and down and swapped so uh to fix it
replace that with
if mouseDelta.Y < 0 then
Anyhow thanks!
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.