- Home /
How to make Input.GetAxis(“Mouse X”) get a value even when the mouse is stopped?
I'm controlling a game object using my mouse horizontally. The object moves left or right depending on the mouse position but as soon as the mouse stops moving the object stops too. What I want is if I drag the mouse to the right side of the screen and stop moving the mouse, then I want the object to move to the right as long as the mouse is in the right half of the screen and vice versa. Currently Input.GetAxis("Mouse X"); returns a 0 value when stopped. Here's the code:
float horizontalInput = Input.GetAxis("Mouse X");
I've already tried using this fix but I don't get the desired result as I'm using mousebuttondown to move my player in the forward direction.
if (Input.GetMouseButton(0) && (horizontalInput == 0f))
{
if (Input.mousePosition.x < Screen.width / 2)
{
horizontalInput = -1f;
}
else if (Input.mousePosition.x > Screen.width / 2)
{
horizontalInput = 1f;
}
}
Is there any other way I can achieve this? Thank you so much for your time!
Can you elaborate more? You are using mouse button 0 to move forward. If you remove the Input.Get$$anonymous$$ouseButton(0)
from the if check, wouldn't it solve your problem?
Answer by Magso · May 15, 2019 at 12:20 PM
It's (horizontalInput == 0f)
that's stopping it because you're altering the variable you're checking, also you don't need the brackets round it.
Your answer
Follow this Question
Related Questions
Tracking slow mouse movements 1 Answer
How do I get a value from Input.GetAxis() ? 1 Answer
Attach an object to the mouse cursor? 0 Answers
2D: How to make Gradius III with a mouse? 1 Answer
Detect Mouse Movement 1 Answer