Keep GameObject position relative to other object

Hello,

I’ve a problem to keep gameObject relative to a line (two objects) depending of their orientation on a surface.
To explain you, the user touch the screen to place two points (A and B) on a horizontal surface in AR.

  • Once the two point A and B are placed on the surface, I place dynamically a point (C) who must stay relative, at same same distance, to the line of point A- B.

I would like to achieve, Point C stay relative to A-B :

My current problem is that C do not keep the position, it comes over A-B.

I Think it’s a question of angle and distance, i’am stuck since 1 weeks, your helps will be really appreciate.
Thanks you.

What exactly are you doing in code?
You could, as a starting direction, get the Vector from A to B and then have it Multiplied with the Quaternion.AngleAxis(90f, Vector3.up). That would rotate the direction by 90 degrees and you could work with that…

1 Like

What you mean by point being parallel to a line? I do not understand.

Thanks for your help,

My segment A-B and the point C were measured in real world from a physical reference.
So i’ve the distance values as scale, and the values modify proportionally depending the distance of A-B when the user place these two points via touch screen.

My reference values

  • Segment A-B = 28 cm
  • Vertical distance of Point C on the segment AB =13 cm
  • Horizontal distance between the segment AB and C = 4 cm

http://zupimages.net/viewer.php?id=19/30/arn3.png

In my code,

  • I first calculate the vertical position of PointC on the scale A-B segment.
  • I calculate the horizontal distance of PointC on A-B segment.
  • I position PointC on X axis + add the horizontal value

// distance of Point C on the Vertical segment A-B
float Vertical_distance_PointC = (13)/28;
Vector3 PointC = Vector3.Lerp(A, B, Vertical_distance_PointC);

// horizontal distance between AB and C
float horizontal_distance_ABtoC = 4 / 28;

// Create Point C vector relative to A-B for parallel distance
Vector3 Point_C_horizontal_Vector = Vector3.Lerp(A, B, horizontal_distance_ABtoC);

// Final float horizontal distance between A-B and C
float distance_horizontal_from_AtoC= Vector3.Distance(A, Point_C_horizontal_Vector);

// Position C + add horizontal value “distance_horizontal_from_AtoC” (parrallel), it’s applied on the x coordinate, but if A-B rotate, it will not keep parallel position.

PointC.transform.position = new Vector3(PointC.transform.position.x+distance_horizontal_from_AtoC, PointC.transform.position.y, PointC.transform.position.z);
PointC.transform.localScale = new Vector3(70.0f, 10.0f, 70.0f);

Result:
It works great if A and B are on same X axis.

In other words, you find a real world plane in VR app and want player to place a point on that plane?

Yes It’s in AR applications (Arcore), The user find a real world plane and place two points on that plane.
The points A and B can be place in all orientations.
Placing these two points works. The point C points who is set dynamically and should keep his position relative to A-B, depending of the orientation of A-B on the surface.

I think we have a language problem. ‘Parallel’ may be the wrong word, because a point cannot be parallel to anything. Perhaps to you mean that it stays in the same position, relative to the line?

Yes exactly, the goal is that the point C stay at the same position, relative to the line (A-B).
even when the line A-B change axis.

Thanks you, I solved my problem.

Vector3 Position_C_on_AB = Vector3.Lerp(A, B, float_distance_of_C_betweenAB); // position of C between A-B
PointC.transform.position = Position_C_on_AB;
PointC.transform.LookAt(PointA.transform);
Vector3 offset = -PointC.transform.forward;
offset *= distance; //distance is your variable of distance
offset = Quaternion.AngleAxis(-90, PointA.transform.up) * offset;
PointC.transform.position = PointA.transform.position + offset;

Thanks you

1 Like