- Home /
How to detect mouse movement as an input
Hi all, I am in the process of converting a project from keyboard only input to keyboard and mouse input. The keyboard is used for vertical and horizontal movement, and the mouse is used for rotation. This is for 2.5d game where rotation only occurs on the X axis. I can get the player object to rotate to follow the mouse already using a raycast from the camera to the mouse pointer. My question is this however:
How can I use a change in the mouses position (i.e if the mouse moves left) to initiate an action. So in pseudo code basically what I am trying to do is this (in UnityScript not C#):
if (Mouseposition moves left)
{
initiate actions;
}
I should mention that I have tried using a variable that compares the initial vector3 position of the mouse (which is updated every 0.1 seconds) with the current position of the mouse but to no avail. The reason I want to do this is so I can use AddTorque
rather than transform.Rotate
I've been toying around with this for a couple of hours now and it's got me stumped. :-(
Any Input would be greatly appreciated. I'm not asking for code, just help with the logic I will need for this.
Answer by save · May 29, 2011 at 04:26 PM
If I understood you correctly you can simply use:
if(Input.GetAxis("Mouse X")<0){
//Code for action on mouse moving left
print("Mouse moved left");
}
if(Input.GetAxis("Mouse X")>0){
//Code for action on mouse moving right
print("Mouse moved right");
}
This is exactly what I needed. $$anonymous$$uch easier and more appropriate than getting the object to follow the mouse. Thanks a lot. P.S Sorry about the late reply. I've been having problems logging in.
Awesome mate! Yeah sometimes we look too deep into a problem to see the easy solution :)
O$$anonymous$$G that feeling when i found this piece of Good Work tnxtnx i was looking for this 4 hours and i couldn't find the right thing, Tnx
Answer by timuther · Jan 06, 2016 at 11:31 AM
bool HasMouseMoved()
{
//I feel dirty even doing this
return (Input.GetAxis("Mouse X") != 0) || (Input.GetAxis("Mouse Y") != 0);
}
That's what I've used but I wonder if there's a more efficient way to get the axis than passing in a string? Seems very clunky.
Answer by shadowpuppet · Jun 17, 2017 at 09:24 PM
OMG! You have no idea how finding this made my week. Yes WEEK. I have a boat controller and when I turn I wanted it to bank into the turn to look more realistic. The code I had worked transform.Rotate(0, 0, -(Input.GetAxis("Mouse X")) Time.deltaTime speed );
but after a few turns left and right , when going straight the boat would still be tilted so spent tons of hours trying to figure out how to right the boat. Storing temp rotation values, animating the bank and trying to trigger those. 3 very long days of failure but THIS!!! this is so simple. If I am steering, the boat banks, if I am not steering, it slerps back to default. THANKS SO MUCH for this amazingly simple yet life saving info
using UnityEngine;
using System.Collections;
public class boatBank : MonoBehaviour {
//this script goes on a child object(boat) of the main gamobject
public float speed = 4f;
public Transform target;//this a child of the main gameobject with same transforms as boat default position this script does not effect it
public float damping = 2f;
void Start () {
}
void Update () {
if(Input.GetAxis("Mouse X")<0)
{
transform.Rotate(0, 0, -(Input.GetAxis("Mouse X")) * Time.deltaTime * speed );
print("Mouse moved left");
}
else if(Input.GetAxis("Mouse X")>0)
{
transform.Rotate(0, 0, -(Input.GetAxis("Mouse X")) * Time.deltaTime * speed );
print("Mouse moved right");
}
else
{
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
print("boat straightening out");
}
}
}
or, the slimmed down version since the first two if statements do the same thing:
using UnityEngine;
using System.Collections;
public class boatBank : $$anonymous$$onoBehaviour {
public float speed = 4f;
public Transform target;
public float damping = 2f;
void Update () {
if(Input.GetAxis("$$anonymous$$ouse X")<0 || (Input.GetAxis("$$anonymous$$ouse X")>0))
{
transform.Rotate(0, 0, -(Input.GetAxis("$$anonymous$$ouse X")) * Time.deltaTime * speed );
print("boat turning");
}
else
{
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
print("boat straightening out");
}
}
Answer by gigmelepcha9 · Aug 15, 2018 at 08:07 AM
Hey Guys can you help me, i am stuck while making my first : First player shooter, i am not able to rotate my screen. i used In Input.GetAxisRaw(Mouse X)
but i am getting 0 ,0,0 as the output
Yeah its Input.GetAxis("Mouse X") not Input.GetAxisRaw(Mouse X)
Your answer
![](https://koobas.hobune.stream/wayback/20220612091816im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Disable mouse input and cursor in game 2 Answers
Anomaly: Mouse Y also affects its X value. Values tied to object position. 2 Answers
How can I treat mouse movement as joystick input? 1 Answer
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
Handle input from serial device 0 Answers