Search Unity

EventTrigger Move issue

Discussion in 'UGUI & TextMesh Pro' started by dreckig-tier, May 11, 2015.

  1. dreckig-tier

    dreckig-tier

    Joined:
    Feb 23, 2013
    Posts:
    9
    Im trying to track mouse move over some object in scene with EventTrigger component (all other events are work) but it does not work =( what i doing wrong, maybe i missed something?
    (on my camera I've added physics raycaster, eventsystem, standalone input modul conponents.).
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    Can you share your project file?
     
  3. dreckig-tier

    dreckig-tier

    Joined:
    Feb 23, 2013
    Posts:
    9
    think i misunderstood what this event do. according to documentation it handles keyboard, not a mouse as i thought.
    but anyway, i have no idea how to use this event, although it is sad that there no event for tracking mouse position change without any button down.
    Project file? You mean sln?
     

    Attached Files:

  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    I see you want to track the mouse position when its over a GUI object?
    Here is some code that I have used to do this in the past:

    Code (csharp):
    1.  
    2. public class TrackMouse: MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
    3. {
    4.     // Called when the pointer enters our GUI component.
    5.     // Start tracking the mouse
    6.     public void OnPointerEnter( PointerEventData eventData )
    7.     {
    8.         StartCoroutine( "TrackPointer" );          
    9.     }
    10.  
    11.     // Called when the pointer exits our GUI component.
    12.     // Stop tracking the mouse
    13.     public void OnPointerExit( PointerEventData eventData )
    14.     {
    15.         StopCoroutine( "TrackPointer" );
    16.     }
    17.  
    18.     IEnumerator TrackPointer()
    19.     {
    20.         var ray = GetComponentInParent<GraphicRaycaster>();
    21.         var input = FindObjectOfType<StandaloneInputModule>();
    22.  
    23.         if( ray != null && input != null )
    24.         {
    25.             while( Application.isPlaying )
    26.             {                  
    27.                 Vector2 localPos; // Mouse position  
    28.                 RectTransformUtility.ScreenPointToLocalPointInRectangle( transform as RectTransform, Input.mousePosition, ray.eventCamera, out localPos );
    29.                      
    30.                 // local pos is the mouse position.
    31.                      
    32.                 yield return 0;
    33.             }      
    34.         }
    35.         else
    36.             UnityEngine.Debug.LogWarning( "Could not find GraphicRaycaster and/or StandaloneInputModule" );      
    37.     }
    38. }
    39.  
     
    Last edited: May 30, 2015
  5. dreckig-tier

    dreckig-tier

    Joined:
    Feb 23, 2013
    Posts:
    9
    actually no, i wanted to use this event for regular objects, but for this code thx.
     
  6. dreckig-tier

    dreckig-tier

    Joined:
    Feb 23, 2013
    Posts:
    9
    oh no, i wanted to track mouse position when i hold button, think your code will help me with that.
     
  7. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    This is probably what you want then: IPointerDownHandler
     
    dreckig-tier likes this.
  8. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    For none UI objects then OnMouseOver could help.
     
    dreckig-tier likes this.
  9. dreckig-tier

    dreckig-tier

    Joined:
    Feb 23, 2013
    Posts:
    9
    thx. will try
     
    karl_jones likes this.
  10. Tzan

    Tzan

    Joined:
    Apr 5, 2009
    Posts:
    736
    Hey free code!

    Vector3 localPos; // Mouse position

    needs to be a Vector2, otherwise its an error.
     
    karl_jones likes this.
  11. Evilbubble

    Evilbubble

    Joined:
    Nov 6, 2012
    Posts:
    1
    It's also missing a semicolon at the end of line 28
    But ignoring that, that's a great snippet, thanks!
     
  12. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    Congratulations, you all passed the test and found my intentional mistakes :D
    Honest ;)
     
  13. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Since this thread still comes up a lot when searching for "what the heck is this useless Move event in EventTrigger, and why can't I get a simple mouse-moved event?", here's my version (based on Karl's code above) that invokes an event whenever the mouse moves over a UI element.

    Code (CSharp):
    1. /* MouseMoveTrigger
    2. Place this on a UI element that you want to cause mouse move events.
    3. For example, it might be an Image (even with 0% opacity) behind all
    4. your other UI elements, to send mouse events for your game.
    5. */
    6. using UnityEngine;
    7. using UnityEngine.Events;
    8. using UnityEngine.EventSystems;
    9. using UnityEngine.UI;
    10. using System.Collections;
    11.  
    12. public class MouseMoveTrigger: MonoBehaviour, IPointerEnterHandler, IPointerExitHandler {
    13.    
    14.     // Event invoked when the mouse moves over this UI element.
    15.     public UnityEvent onMouseMove;
    16.        
    17.     // Called when the pointer enters our GUI component.
    18.     // Start tracking the mouse
    19.     public void OnPointerEnter( PointerEventData eventData ) {
    20.         StartCoroutine("TrackPointer");
    21.     }
    22.    
    23.     // Called when the pointer exits our GUI component.
    24.     // Stop tracking the mouse
    25.     public void OnPointerExit( PointerEventData eventData ) {
    26.         StopCoroutine("TrackPointer");
    27.     }
    28.    
    29.     IEnumerator TrackPointer() {
    30.         Vector3 mousePos = Vector3.zero;
    31.         while (Application.isPlaying) {
    32.             if (Input.mousePosition != mousePos) {
    33.                 if (onMouseMove != null) onMouseMove.Invoke();
    34.                 mousePos = Input.mousePosition;
    35.             }
    36.             yield return 0;
    37.         }
    38.     }
    39.  
    40. }
     
    GameDevMig, S3rD4n73 and karl_jones like this.
  14. tra

    tra

    Joined:
    Mar 21, 2013
    Posts:
    32
    JoeStrout and karl_jones like this.