Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Jun 15, 2013 at 11:59 AM by Eugenius for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Eugenius · May 07, 2013 at 11:49 PM · mouseposition

How to get the mouse direction while left click is pressed?

Hi again guys.

I've stumbled a bit in the code and I've had over 8 hours of trying to get this today but I couldn't get to the end of it. I've searched all over and found similar questions but none offered me a good response honestly.

I want to click on an object and while holding the click to know if my mouse is moving up, down, left or right so that afterwards I could perform actions based on these 4 directions.

I've tried to debug it like this, but it doesn't quite get me there. Any help would be greatly appreciated (even a link towards a question that has been answered if this is a dupe):

 function Update()
 {
     if(Input.GetMouseButtonDown(0))
     {
     OnMouseDrag();
     }
 }
 
 function OnMouseDrag(){
 
     if(Input.GetAxis("Mouse X") > 0)
     {
         Debug.Log("X e mai mare ca 0");
     }
     
     if(Input.GetAxis("Mouse X") < 0)
     {
         Debug.Log("X mai mic ca 0");
     }
     
     if(Input.GetAxis("Mouse Y") > 0)
     {
         Debug.Log("y mare ca 0");
     }
     
     if(Input.GetAxis("Mouse Y") < 0)
     {
         Debug.Log("y mic ca 0");
     }
 }
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

1 Reply

  • Sort: 
avatar image
2
Best Answer

Answer by robertbu · May 08, 2013 at 12:39 AM

Here is one solution using Vector3.Dot(). There are efficiency improvements that can be made:

 #pragma strict
 
 private var v3Pos : Vector3;
 private var threshold = 9;
 
 function OnMouseDown() {
     v3Pos = Input.mousePosition;
 }
 
 function OnMouseDrag() {
     var v3 = Input.mousePosition - v3Pos;
     v3.Normalize();
     var f = Vector3.Dot(v3, Vector3.up);
     if (Vector3.Distance(v3Pos, Input.mousePosition) < threshold) {
         Debug.Log("No movement");
         return;
     }
 
     if (f >= 0.5) {
        Debug.Log("Up");
     }
     else if (f <= -0.5) {
         Debug.Log("Down");
     }
     else {
         f = Vector3.Dot(v3, Vector3.right);
         if (f >= 0.5) {
             Debug.Log("Right");
         }
         else {
                 Debug.Log("Left");
         }
     }
     v3Pos = Input.mousePosition;
 }
Comment
Add comment · Show 3 · 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 Eugenius · May 08, 2013 at 08:08 PM 0
Share

Thanks, that's useful but not quite what I need here. I only want it to get my direction based on my first click location. Therefore, when I move the mouse upwards from the initial position do something, when i move the mouse left from the initial position do something else and so on.

avatar image robertbu · May 08, 2013 at 09:58 PM 1
Share

If you take out line 34, this will report the relative position based on the current mouse position and the starting position. But that does not sound like what you want. You are going to have to be a lot more explicit. So if I move up three units and right two units, what should be reported? And if I then move back left one unit (I'm still right of the starting position) what should be reported? And do you need this reporting during the entire drag, or only during an On$$anonymous$$ouseUp().

avatar image Eugenius · May 08, 2013 at 10:31 PM 0
Share

Yes! Thank you, that was exactly what I needed! This works for my specific problem without line 34! $$anonymous$$arking as correct! Thank you for the help!

Follow this Question

Answers Answers and Comments

12 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 avatar image avatar image

Related Questions

how to have a sprite follow the spot of my mouse cursor. 0 Answers

Creating platforms based on mouse position 0 Answers

Smoother Rotation with mouse 2 Answers

How to make gun rotate around player based on mouse position 2 Answers

Sprite animation dependant on object direction 2 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