- Home /
How can I treat mouse movement as joystick input?
Is there a way to make mouse movement act like a joystick, including the dead zone? So, if the mouse is moved away from the middle of the screen I can make an object move?
For example, I have a space ship and if the mouse is in (or near) the middle of the screen, it will keep facing forward. But if it is moved to the left/right, or top/bottom of the screen, the ship will start rotating. (And will keep rotating until the mouse is returned close to the middle. (There will probably be an onscreen crosshair or similar indicating the mouse position)
Thanks!
Answer by Arycama · Jul 02, 2014 at 08:51 AM
Actually nevermind, I figured out an easy way to achieve what I wanted. For anyone else interested, I simply made two float variables within the movement script, called "mousePosX" and "mousePosY", and set them to 0. Then in the Update function, I set mousePosX += Input.GetAxis("Mouse X"), and the same for mouse Y, which I then used to control adding torque along the x and y axes.
Thanks for the help!
I am trying to replicate this but I am unable to add my flat to get axis..
using UnityEngine; using System.Collections;
public class CubeControl : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
float mousePosX = 0;
float mousePosY = 0;
}
// Update is called once per frame
void Update () {
float mousePosX = Input.GetAxis ("$$anonymous$$ouse X") + mousePosX;
float mousePosY = Input.GetAxis("$$anonymous$$ouse Y") + mousePosY;
var x = Input.GetAxis ("$$anonymous$$ouse X");
var y = Input.GetAxis ("$$anonymous$$ouse Y");
transform.Rotate (mousePosX, 0f, mousePosY);
}
}
Answer by tanoshimi · Jul 01, 2014 at 05:37 PM
Yes, you can, using Input.mousePosition.
Thanks, it doesn't work exactly as I'd like though. $$anonymous$$y current input is basically pitch = Input.getAxis("$$anonymous$$ouse Y") yaw = Input.getAxis("$$anonymous$$ouse X"), then Rigidbody.addRelativeTorque(pitch, yaw, roll). If I connect a game pad or joystick and map it to the pitch and yaw variables, I get smooth consistent movement when it is pressed. However, with a mouse it requires constant moving.
Input.mousePosition would require a lot of extra scripting for a deadzone and lots of greater than/less than checks to see what direction it moves. However, I'll keep working with Input.mousePosition to try and figure out a solution, unless someone else can chip in.
Your answer
Follow this Question
Related Questions
Simulate a mouse Click? 2 Answers
How could I use the mouse as input for a flight control system (and space bar for thrust!) 2 Answers
Implementing a virtual joystick with mouse 1 Answer
Disable mouse input and cursor in game 2 Answers
Keyboard/Joystick Inputs Do Not Work until Mouse Clicked 0 Answers