Search Unity

Dragging object along a line

Discussion in 'Scripting' started by XukeLho, Aug 27, 2015.

  1. XukeLho

    XukeLho

    Joined:
    Dec 30, 2014
    Posts:
    199
    Hi guys.
    Before you read the problem, please refer to the attached image in order to understand it better.

    I have this situation. I must be able to drag a gameobject along a perpendicular line, created by 2 Vector3 points.
    As you can see in the image, the points A and B (both are Vector3) are what I use to limit my gameobject movements.

    The gameobject starts exatly between points A and B (I use
    Code (CSharp):
    1. Lerp(A, B, .5f)
    to put it there) but I can't limit its drag movement to the perpendicular line.

    Any sugestions?
     

    Attached Files:

  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    http://wiki.unity3d.com/index.php/3d_Math_functions
    see function for getting a PointOnLine
    You project the mouse click position onto the line which is perpendicular to AB
    to calculate points C and D on the perp line, you have
    Code (CSharp):
    1. C = (A+B)/2
    2. and
    3. vector2 dir = B-A
    4. Vector2 D = C + Vector2(dir.y, -dir.x)
     
  3. XukeLho

    XukeLho

    Joined:
    Dec 30, 2014
    Posts:
    199
    Can't get it to work. Not sure if or where I'm doing something wrong. Can you be more specific with your code please?
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,536
    can you explain why?
     
  5. XukeLho

    XukeLho

    Joined:
    Dec 30, 2014
    Posts:
    199
    I don't understand your question. You want me to explain why I can't do this? Because I'm not smart enough I guess...
     
  6. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,536
    I didn't mean like that, of course, I meant more what specifically are you stuck on? Is it the math idea to do this? Is it the touch interaction portion? Just looking for some more context around 'why' so there is a clear approach to the problem.
     
  7. XukeLho

    XukeLho

    Joined:
    Dec 30, 2014
    Posts:
    199
    What I'm stuck on is on how to limit the cube that I want to drag, to the perpendicular line of A-B.
    Currently I can drag the cube around anywhere with this code:
    Code (CSharp):
    1. private Vector3 screenPoint;
    2.     private Vector3 offset;
    3.  
    4.  
    5.     void OnMouseDown()
    6.     {
    7.         screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
    8.        
    9.         offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
    10.     }
    11.  
    12.  
    13.     void OnMouseDrag()
    14.     {
    15.         Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
    16.        
    17.         Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
    18.  
    19. transform.position = curPosition;
    20.     }
    But as I said, that code allows me to move it anywhere, and that is not what I am looking for.
     
  8. XukeLho

    XukeLho

    Joined:
    Dec 30, 2014
    Posts:
    199
    This is a perfect example of what I need but, It limits the drag to a line segment. And I need an infinite line.

    Code (CSharp):
    1. public Point _pointOnLine(Point pt1, Point pt2, Point pt)
    2.         {
    3.             bool isValid = false;
    4.  
    5.             var r = new Point(0, 0);
    6.             if (pt1.Y == pt2.Y && pt1.X == pt2.X) { pt1.Y -= 0.00001; }
    7.  
    8.             var U = ((pt.Y - pt1.Y) * (pt2.Y - pt1.Y)) + ((pt.X - pt1.X) * (pt2.X - pt1.X));
    9.  
    10.             var Udenom = Math.Pow(pt2.Y - pt1.Y, 2) + Math.Pow(pt2.X - pt1.X, 2);
    11.  
    12.             U /= Udenom;
    13.  
    14.             r.Y = pt1.Y + (U * (pt2.Y - pt1.Y));
    15.             r.X = pt1.X + (U * (pt2.X - pt1.X));
    16.  
    17.             double minx, maxx, miny, maxy;
    18.  
    19.             minx = Math.Min(pt1.Y, pt2.Y);
    20.             maxx = Math.Max(pt1.Y, pt2.Y);
    21.  
    22.             miny = Math.Min(pt1.X, pt2.X);
    23.             maxy = Math.Max(pt1.X, pt2.X);
    24.  
    25.             isValid = (r.Y >= minx && r.Y <= maxx) && (r.X >= miny && r.X <= maxy);
    26.  
    27.             return isValid ? r : new Point();
    28.         }
     
  9. XukeLho

    XukeLho

    Joined:
    Dec 30, 2014
    Posts:
    199
    Anyone?