Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Detect collision with OnMouseDrag

Discussion in '2D' started by Ranbe, Jan 25, 2016.

  1. Ranbe

    Ranbe

    Joined:
    Jan 25, 2016
    Posts:
    13
    Hello,
    i have those two functions:
    http://pastebin.com/XJhR4SEd
    who allows me to drag my object through the x axis
    but i have one problem , i want the object to be stopped when colliding with another object/stop when it about to be outside the camera(the camera position is constant)
    i can't figure out how to do it , appreciate any help ty :)
     
  2. Rostam24

    Rostam24

    Joined:
    Mar 5, 2014
    Posts:
    119
    You could add a bool canDrag, which is set to true OnMouseDown, and to false when colliding. Then in the OnMouseDrag function, simply return if the canDrag is false.

    Then in some kind of update function, you could check to see if the object is about to be outside of the camera, and if so: set canDrag to false.