- Home /
Connecting an object to 2 others
I'm trying to develop an android game. There are 2 spheres in the game the user can drag around. I have a plane in the middle. I have set the plane to constantly adapt the length to the distance between the two spheres. I want the line to basically be attatched to the two spheres at either end, and when you move one, it will look like you're just stretching it out (but not thinning). The problem is, transform.LookAt doesn't work because it pivots around the middle, not the sphere the user isn't dragging. Anyone got a way to do this?
Answer by MikeNewall · May 29, 2014 at 04:33 AM
In the scene view create a quad and rotate it 90 degrees in the X axis. Set the X scale to however wide you want the plane to be.
You need to make sure the pivot of the plane is at its edge, so translate it -0.5 in the Z axis, then create an empty game object (at the world origin) and parent the quad to it.
The script to control the direction of the plane is really simple
public Transform pointA, pointB;
public GameObject plane;
void Update(){
plane.transform.position = pointA.position;
plane.transform.LookAt(pointB.position);
plane.transform.localScale = new Vector3(1, 1, Vector3.Distance(pointA.position, pointB.position));
}
You set the position of the plane to always be at the same position as pointA. We moved the pivot so this will align one edge with pointA. Then you use transform.LookAt to look at pointB, which will rotate around the pivot constrained to pointA.
Because the quad has a scale of 1 in Z 1 unit in scale equates to one world unit so you can use the distance between the points to work out the new scale.
Hope that helped.
Mike
Thanks, but I figured it out. I forgot to add the transforms of the two and divide it (then set the middle object to that). Thanks though
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How Cast a ray between two points and return all points intercepted? 0 Answers
how do i Disable and Enable buttons? 2 Answers
Unity: C#: Eyes look at mouse: HELP!! 3 Answers
Distribute terrain in zones 3 Answers