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 /
avatar image
0
Question by opdday2 · Mar 05, 2014 at 06:18 PM · mouseclickmouse-dragmousemove

Why does this not detect mouse movement?

I have tried many ways to detect mouse movement and have been S.O.L. getting anywhere with it. I can detected the current location and compare it to the original location, but this doesn't seem to work for detecting mouse movement. I tried get axis and that isn't working either. Am I doing something wrong?

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class UserInput : MonoBehaviour {
 
     Vector2 beginMousePos;
     Vector2 currMousePos;
     bool mouseIsSet = false;
     bool mouseHasMoved = false;
     RaycastHit hit;
     bool UnitsAreSelected = false;
     bool structSelected= false;
 
     // Unit Menu Option Icons
     Vector2 attackIconPos;
     Vector2 patrolIconPos;
     Vector2 followIconPos;
     Vector2 loadIconPos;
     Vector2 unloadIconPos;
     public Texture2D attactIcon;
     public Texture2D patrolIcon;
     public Texture2D followIcon;
     public Texture2D loadIcon;
     public Texture2D unloadIcon;
 
     // Use this for initialization
     void Start () {
         attackIconPos = new Vector2((Screen.width/15), (Screen.height/9)*7);
         patrolIconPos = new Vector2((Screen.width/15)*2, (Screen.height/9)*7);
         followIconPos = new Vector2((Screen.width/15)*3, (Screen.height/9)*7);
         loadIconPos = new Vector2((Screen.width/15), (Screen.height/9)*8);
         unloadIconPos = new Vector2((Screen.width/15)*2, (Screen.height/9)*8);
     }
     
     // Update is called once per frame
     void Update () {
         if(Input.GetMouseButtonDown (0))
         {
             if (!mouseIsSet)
             {
                 Debug.Log (mouseIsSet);
                 beginMousePos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
                 mouseIsSet = true;
             }
             if(Input.GetAxis("Mouse X") < 0)
             {
                 Debug.Log ("movedmouse");
                 mouseHasMoved = true;
             }
             else if (Input.GetAxis("Mouse X") > 0)
             {
                 Debug.Log ("movedmouse");
                 mouseHasMoved = true;
             }
             else if(Input.GetAxis("Mouse Y") < 0)
             {
                 Debug.Log ("movedmouse");
                 mouseHasMoved = true;
             }
             else if (Input.GetAxis("Mouse Y") > 0)
             {
                 Debug.Log ("movedmouse");
                 mouseHasMoved = true;
             }
 
         }
         if(Input.GetMouseButtonUp (0))
         {
             if (mouseHasMoved)
             {
                 mouseIsSet = false;
                 mouseHasMoved = false;
                 currMousePos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
                 UnitManager.FindSelectedUnits(beginMousePos, currMousePos);
                 if (UnitManager.Selected.Count > 0)
                 {
                     UnitsAreSelected = true;
                 }
                 else
                 {
                     UnitsAreSelected = false;
                 }
             }
             else
             {
                 mouseIsSet = false;
             }
         }
 
         if(Input.GetMouseButtonDown (1))
         {
 
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast (ray, out hit))
             {
                 Unit unitHit = hit.collider.gameObject.GetComponentInChildren<Unit>();
                 if(unitHit != null)
                 {
                     UnitManager.MoveActSelectedUnits(unitHit);
                 }
                 else
                 {
                     UnitManager.MoveSelectedUnits (hit.point);
                 }
 
             }
         }
 
     }
 
     void OnGUI()
     {
         if (UnitsAreSelected)
         {
             if (!structSelected)
             {
                 // Attack
                 if (GUI.Button(new Rect(attackIconPos.x, attackIconPos.y, Screen.width/15, Screen.height/9), attactIcon))
                 {
                     
                 }
                 
                 // Patrol
                 if (GUI.Button(new Rect(patrolIconPos.x, patrolIconPos.y, Screen.width/15, Screen.height/9), attactIcon))
                 {
                     
                 }
                 
                 // Follow
                 if (GUI.Button(new Rect(followIconPos.x, followIconPos.y, Screen.width/15, Screen.height/9), attactIcon))
                 {
                     
                 }
                 
                 // Load
                 if (GUI.Button(new Rect(loadIconPos.x, loadIconPos.y, Screen.width/15, Screen.height/9), attactIcon))
                 {
                     
                 }
                 
                 // UnLoad
                 if (GUI.Button(new Rect(unloadIconPos.x, unloadIconPos.y, Screen.width/15, Screen.height/9), attactIcon))
                 {
                     
                 }
             }
             else
             {
                 
             }
         }
     }
     
 }
 
Comment
Add comment · Show 2
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 Mulldor · Mar 05, 2014 at 06:26 PM 0
Share

Ins$$anonymous$$d of checking both X and Y to see if your mouse has moved, use Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); Store the first click in a Vector2 then make an ifstatement with Vector2.Distance(originalPos, newPos) > 0 - $$anonymous$$ouseHas$$anonymous$$oved = true then set new location to newPos, think that will solve your problem! Then create a rectangle to select units between your originalPos and your newPos!

avatar image opdday2 · Mar 05, 2014 at 06:48 PM 0
Share

I tried this originally, Just in case I did something wrong I will try again. Thanks for the quick reply

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by opdday2 · Mar 05, 2014 at 08:21 PM

ok, so Now I have this and it doesn't seem to even run logic in GetMouseButtonDown(0) here is what I have now and the debug.log at beginning of GetMouseButtonDown(0) is never called. using UnityEngine; using System.Collections; using System.Collections.Generic;

 public class UserInput : MonoBehaviour {
 
     Vector2 beginMousePos;
     Vector2 currMousePos;
     bool mouseIsSet = false;
     RaycastHit hit;
     bool UnitsAreSelected = false;
     bool structSelected= false;
 
     // Unit Menu Option Icons
     Vector2 attackIconPos;
     Vector2 patrolIconPos;
     Vector2 followIconPos;
     Vector2 loadIconPos;
     Vector2 unloadIconPos;
     public Texture2D attactIcon;
     public Texture2D patrolIcon;
     public Texture2D followIcon;
     public Texture2D loadIcon;
     public Texture2D unloadIcon;
 
     // Use this for initialization
     void Start () {
         attackIconPos = new Vector2((Screen.width/15), (Screen.height/9)*7);
         patrolIconPos = new Vector2((Screen.width/15)*2, (Screen.height/9)*7);
         followIconPos = new Vector2((Screen.width/15)*3, (Screen.height/9)*7);
         loadIconPos = new Vector2((Screen.width/15), (Screen.height/9)*8);
         unloadIconPos = new Vector2((Screen.width/15)*2, (Screen.height/9)*8);
     }
     
     // Update is called once per frame
     void Update () 
     {
         if(Input.GetMouseButtonDown (0))
         {
             Debug.Log ("buttonDowm");
             if (!mouseIsSet)
             {
                 beginMousePos = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, 
                                                 Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
                 Debug.Log (beginMousePos);
                 mouseIsSet = true;
             }
         }
         if(Input.GetMouseButtonUp (0))
         {
             if (mouseHasMoved())
             {
                 currMousePos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
                 UnitManager.FindSelectedUnits(beginMousePos, currMousePos);
                 if (UnitManager.Selected.Count > 0)
                 {
                     UnitsAreSelected = true;
                 }
                 else
                 {
                     UnitsAreSelected = false;
                 }
                 mouseIsSet = false;
             }
             else
             {
                 mouseIsSet = false;
             }
         }
 
         if(Input.GetMouseButtonDown (1))
         {
 
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast (ray, out hit))
             {
                 Unit unitHit = hit.collider.gameObject.GetComponentInChildren<Unit>();
                 if(unitHit != null)
                 {
                     UnitManager.MoveActSelectedUnits(unitHit);
                 }
                 else
                 {
                     UnitManager.MoveSelectedUnits (hit.point);
                 }
 
             }
         }
 
     }
 
     void OnGUI()
     {
         if (UnitsAreSelected)
         {
             if (!structSelected)
             {
                 // Attack
                 if (GUI.Button(new Rect(attackIconPos.x, attackIconPos.y, Screen.width/15, Screen.height/9), attactIcon))
                 {
                     
                 }
                 
                 // Patrol
                 if (GUI.Button(new Rect(patrolIconPos.x, patrolIconPos.y, Screen.width/15, Screen.height/9), attactIcon))
                 {
                     
                 }
                 
                 // Follow
                 if (GUI.Button(new Rect(followIconPos.x, followIconPos.y, Screen.width/15, Screen.height/9), attactIcon))
                 {
                     
                 }
                 
                 // Load
                 if (GUI.Button(new Rect(loadIconPos.x, loadIconPos.y, Screen.width/15, Screen.height/9), attactIcon))
                 {
                     
                 }
                 
                 // UnLoad
                 if (GUI.Button(new Rect(unloadIconPos.x, unloadIconPos.y, Screen.width/15, Screen.height/9), attactIcon))
                 {
                     
                 }
             }
             else
             {
                 
             }
         }
     }
 
     bool mouseHasMoved()
     {
         if(Vector2.Distance(beginMousePos, new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, 
                                                             Camera.main.ScreenToWorldPoint(Input.mousePosition).y)) > 0)
         {
             Debug.Log ("movedmouse");
             return true;
         }
         else
         {
             return false;
         }
     }
     
 }
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 opdday2 · Mar 05, 2014 at 08:25 PM 0
Share

the logic in Get$$anonymous$$ouseButtonDown (1) RUNS FINE

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

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

Related Questions

Why is OnDrag triggering but not OnMouseDown? 0 Answers

Can I change GUIButton behaviour? 1 Answer

Prevent Input.GetMouseButtonDown from working anywhere. 3 Answers

How can I move my 2D main player up and down by clicking and dragging him up or down? 1 Answer

Click and drag to rotate an object without changing its position. 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