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): Lerp(A, B, .5f) to put it there) but I can't limit its drag movement to the perpendicular line. Any sugestions?
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): C = (A+B)/2 and vector2 dir = B-A Vector2 D = C + Vector2(dir.y, -dir.x)
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?
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...
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.
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): private Vector3 screenPoint; private Vector3 offset; void OnMouseDown() { screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position); offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); } void OnMouseDrag() { Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset; transform.position = curPosition; } But as I said, that code allows me to move it anywhere, and that is not what I am looking for.
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): public Point _pointOnLine(Point pt1, Point pt2, Point pt) { bool isValid = false; var r = new Point(0, 0); if (pt1.Y == pt2.Y && pt1.X == pt2.X) { pt1.Y -= 0.00001; } var U = ((pt.Y - pt1.Y) * (pt2.Y - pt1.Y)) + ((pt.X - pt1.X) * (pt2.X - pt1.X)); var Udenom = Math.Pow(pt2.Y - pt1.Y, 2) + Math.Pow(pt2.X - pt1.X, 2); U /= Udenom; r.Y = pt1.Y + (U * (pt2.Y - pt1.Y)); r.X = pt1.X + (U * (pt2.X - pt1.X)); double minx, maxx, miny, maxy; minx = Math.Min(pt1.Y, pt2.Y); maxx = Math.Max(pt1.Y, pt2.Y); miny = Math.Min(pt1.X, pt2.X); maxy = Math.Max(pt1.X, pt2.X); isValid = (r.Y >= minx && r.Y <= maxx) && (r.X >= miny && r.X <= maxy); return isValid ? r : new Point(); }