Search Unity

Question Using the Event System

Discussion in 'Getting Started' started by adamh18, Mar 5, 2022.

  1. adamh18

    adamh18

    Joined:
    Jan 31, 2022
    Posts:
    1
    I have just started a little 2D project where I am using the mouse to drag a Game Object around the scene. I have the Standalone Input Module and Event System in my scene and my GO implements the IDragHandler interface and I move the GO in response to OnDrag events from the event system.

    My problem is that I would also like to be able to pan around the scene using the same "click and drag" behaviour. The problem is I often end up with simultaneous panning of the camera and dragging of the GO. I think I must be using events/inputs not in the intended way so hopefully someone can point out an approach I should be following. I will list below the things I have tried:
    • Using a component doing stuff like
      Code (CSharp):
      1. if (Input.GetMouseButtonDown(0) )
      2.         {
      3.             touchStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);
      4.         }
      5.  
      6.         if (Input.GetMouseButton(0))
      7.         {
      8.             Vector3 delta = touchStart - Camera.main.ScreenToWorldPoint(Input.mousePosition);
      9.             Camera.main.transform.position += delta;
      10.         }
      in the update loop to implement panning. However, the panning behaviour still happens when I'm trying to drag something. Is it possible to detect if there is an OnDrag event being handled elsewhere and then I can just pan when there isn't something else happening?
    • Putting a panel in my UI which can then accept the OnDrag events - this stopped the game objects I am trying to drag from responding to OnDrag events, I guess because the panel swallowed them up. I tried using different layers to get around this, e.g. my panel in a lower numbered layer than my other Game Objects but that didn't change the behaviour
    • Putting a big game object behind everything as a background. This gives the behaviour I would like but obviously puts a limit on where the solution works so I would have to put hard limits on how far someone could pan/zoom. That's probably not an issue in reality but it just doesn't feel like the right solution.
    • Create a new component to handle mouse inputs and send custom events to the game object or panning component as appropriate. I feel like this could create problems down the line if say, I ever wanted to drag UI elements around with the other event system which otherwise works nicely for clicking on UI buttons and the like.
    I don't know Unity that well so is there so I suspect there could be some different and far more appropriate way of achieving what I want. I'm not so far down the project that I couldn't completely change how I handle stuff so I'd quite like to start out with the right approach rather than have to apply a lot of sticking plasters later on.

    All comments, pointers and suggestions gratefully received.

    Thanks