How do I tell if I lean the mouse?

I want to be able to know how to tell if i lean my mouse to the side.

Im trying to make a plane. And I want it so when you lean your mouse to the side you will turn that way.
How would I do this

Another way to think about it is how to detect when the mouse is close to the side of the screen

1 Like

Most computer mice cannot tell if they are leaning. You might be able to use the Gyroscope on a phone and UserInputService but a normal computer mouse cannot do this.

1 Like

Using the mouse’s position and the center of the screen, you can map it into a -1 to 1 coordinate system that tells you both the direction and the “strength” of the leaning

1 Like

How would I do this? Im really bad at understanding stuff

Ok so this is the basic idea. The center of the screen would be (0, 0), the top right corner is (1, 1), and the bottom left would be (-1, -1).

You can use the linear interpolation formula to figure out what the coordinate should be for any position on the screen. This is the formula:

goal = start + alpha*(finish - start)

But we need to find alpha, not goal. We know what everything else should be. goal is the position of the mouse, start is always going to be (0, 0) since that’s where the screen starts, and finish is just the screen size.

To get alpha, we can do some simple algebra:

goal - start = alpha*(finish - start) --move start to the other side
(goal - start)/(finish - start) = alpha*(finish - start)/(finish - start) --divide both sides by (finish - start)
alpha = (goal - start)/(finish - start)

Since start is always going to be zero, we can actually leave it out of the equation:

alpha = goal / finish

And now we just need to plug in the variables. We will need to use this formula for each dimension.

local goal = UserInputService:GetMouseLocation()
local finish = workspace.CurrentCamera.ViewportSize

local x_alpha = goal.X / finish.X
local y_alpha = goal.Y / finish.Y

But a few more things, alpha is in the range [0, 1]. We need to convert to [-1, 1].

--first turn it into [-0.5, 0.5] by shifting it down, then multiply by two to turn it into [-1, 1]
local x_alpha = ((goal.X / finish.X) - 0.5) * 2
local y_alpha = ((goal.Y / finish.Y) - 0.5) * 2

In addition, the Y value is flipped since 0 is the top left when it should be at the bottom. We have to invert it by doing 1 - alpha.

local y_alpha = (1 - (goal.Y / finish.Y) - 0.5) * 2
--this simplifies to
local y_alpha = (0.5 - goal.Y / finish.Y) * 2

Now we have the actual value.

local lean = Vector2.new(x_alpha, y_alpha)
2 Likes

One question.
Is the start = vector2, or is it equal to something else? I dont really l know how it works.

I also noticed how you do alpha = (goal - start)/(finish - start)
The thing is I dont have “GOAL” cause you never declare when I set it.

So I dont know how to do this.

I have it all written down in the post. Please read the entire thing.