A short tutorial on finding a midpoint between 2 objects.
You may want a player to fire a weapon that goes towards the center point of 2 other objects or you may want to instantiante and object between 2 objects…
I will use the example of drawing a line from a cube C to the center point between 2 spheres A B in 3D space.
So create a new C# script and attach it to the cube C
You will need the Transform of the 2 objects you want to find the midpoint of so you can either assign these in the inspector or ‘find’ them:
public Transform OtherObjectA; // First object of pair
public Transform OtherObjectB; // Second object of pair
Now you need to create a vector from C to A and from C to B
To get the vector that points from a source to a target you subtract the source vector from the target vector:
target.position - start.position
Now in this example we need to do this 2 times, once for C to A and once for C to B
so vector C to A is A.position - C.position
and vector C to B is B.position - C.position
Vector3 directionCtoA = OtherObjectA.position - transform.position; // directionCtoA = positionA - positionC
Vector3 directionCtoB = OtherObjectB.position - transform.position; // directionCtoB = positionB - positionC
Now all you need to do is use the midpoint formula http://www.purplemath.com/modules/midpoint.htm
so in our example we use:
MidPoint = ((pointA.x + pointB.x)/2, (pointA.y + pointB.y)/2, (pointA.z + pointB.z)/2)
and to assign this to a new vector in Unity:
Vector3 midpointAtoB = new Vector3((directionCtoA.x+directionCtoB.x)/2.0f,(directionCtoA.y+directionCtoB.y)/2.0f,(directionCtoA.z+directionCtoB.z)/2.0f);
And thats it.
using UnityEngine;
using System.Collections;
public class HereToMidpointOfAB : MonoBehaviour
{
public Transform OtherObjectA; // First object of pair
public Transform OtherObjectB; // Second object of pair
public void Update ()
{
Vector3 directionCtoA = OtherObjectA.position - transform.position; // directionCtoA = positionA - positionC
Vector3 directionCtoB = OtherObjectB.position - transform.position; // directionCtoB = positionB - positionC
Vector3 midpointAtoB = new Vector3((directionCtoA.x+directionCtoB.x)/2.0f,(directionCtoA.y+directionCtoB.y)/2.0f,(directionCtoA.z+directionCtoB.z)/2.0f); // midpoint between A B this is what you want
}
}
Here is an example using the above code to draw debug rays showing all these vectors:
The 2 green lines are C to A and C to B
The red line just shows the line joining objects A and B
The blue line is the result we wanted and is the line that always points from the source to the center of the space between objects A and B
Here is the script that shows these lines:
using UnityEngine;
using System.Collections;
public class HereToMidpointOfAB : MonoBehaviour
{
public Transform OtherObjectA; // First object of pair
public Transform OtherObjectB; // Second object of pair
public void Update ()
{
Vector3 directionCtoA = OtherObjectA.position - transform.position; // directionCtoA = positionA - positionC
Vector3 directionCtoB = OtherObjectB.position - transform.position; // directionCtoB = positionB - positionC
Vector3 directionAtoB = OtherObjectB.position - OtherObjectA.position; // directionAtoB = target.position - source.position
Vector3 midpointAtoB = new Vector3((directionCtoA.x+directionCtoB.x)/2.0f,(directionCtoA.y+directionCtoB.y)/2.0f,(directionCtoA.z+directionCtoB.z)/2.0f); // midpoint between A B
Debug.DrawRay(OtherObjectA.position, directionAtoB, Color.red); // line between A and B
Debug.DrawRay(transform.position, directionCtoA, Color.green); // line between C and A
Debug.DrawRay(transform.position, directionCtoB, Color.green); // line between C and B
Debug.DrawRay(transform.position, midpointAtoB, Color.blue); // line midway between C and B
}
}
And here is the full project, just open the blank scene and move the objects about.
1479721–81858–$MidpointBetween2Objects.unitypackage (5.63 KB)
Any questions just ask.