Search Unity

Find mouse distance from last location?

Discussion in 'Scripting' started by Shadowing, Jan 12, 2020.

  1. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Trying to detect the mouse distance between two mouse locations.
    This below is giving me radical numbers. Some times its 18 other times 130 and i hardly move the mouse.
    I must not be doing it correctly?


    Code (csharp):
    1.  
    2. if(Vector2.Distance(LastMousePostion, Input.mousePosition) > .1){
    3.  
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi @Shadowing,

    How are you calculating/setting the last mouse position? Maybe you do it in wrong place or at a wrong time.

    You probably should set it every update and then compare it in the next update to the current mouse position.
     
  3. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Thanks for the response @Olmi
    Like this below.

    I hover my mouse over a area. it trips runs the Co-routine for 1 sec and if the mouse has moved to far then don't show the pop up window.

    Code (csharp):
    1.  
    2.         if(!Input.GetKey(KeyCode.Mouse0) && !Input.GetKey(KeyCode.Mouse1)){
    3.             LastMousePostion = Input.mousePosition;
    4.             QuestDetailsCoroutine = StartCoroutine(ShowQuestDetails(questInformation, quest));
    5.         }
    6.  
    Code (csharp):
    1.  
    2.     IEnumerator ShowQuestDetails(JSONNode questInformation,Quest quest){
    3.         yield return new WaitForSeconds(1.0f);
    4.  
    5.         if(Vector2.Distance(LastMousePostion, Input.mousePosition) > .1){
    6.             LastMousePostion = Vector2.zero;
    7.         } else {
    8.             LastMousePostion = Vector2.zero;
    9.             DisplayQuestDetails(questInformation, quest,true);
    10.         }
    11.      
    12.     }
    13.  
    14.