Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by OoglyWoogly · May 29, 2011 at 04:03 PM · inputmouse position

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
37
Best Answer

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");
 }
Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image OoglyWoogly · Jun 03, 2011 at 12:40 PM 0
Share

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.

avatar image save · Jun 03, 2011 at 06:32 PM 1
Share

Awesome mate! Yeah sometimes we look too deep into a problem to see the easy solution :)

avatar image tayyab43 · Nov 10, 2013 at 12:18 PM 0
Share

Is this exactly the same in c#?

avatar image FLASHDENMARK · Nov 10, 2013 at 01:39 PM 0
Share

Yes it is.

avatar image _Grocfex0000 · Oct 25, 2014 at 06:13 PM 0
Share

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

Show more comments
avatar image
6

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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");
             
         }
         
     }
 }   
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image shadowpuppet · Jun 17, 2017 at 09:31 PM 0
Share

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");
             
         }
     }
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hello_I_Am_A_Robot · Jun 03 at 10:24 AM 0
Share

Yeah its Input.GetAxis("Mouse X") not Input.GetAxisRaw(Mouse X)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

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 to move an object in 4 directions by tilting device when not parallel to the ground 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges