Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Find midpoint between 2 game objects C#

Discussion in 'Community Learning & Teaching' started by softwizz, Jan 10, 2014.

  1. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    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
    $problem.jpg

    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:
    Code (csharp):
    1. public Transform OtherObjectA; // First object of pair
    2. 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

    Code (csharp):
    1. Vector3 directionCtoA = OtherObjectA.position - transform.position; // directionCtoA = positionA - positionC
    2. 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:
    Code (csharp):
    1. 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.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HereToMidpointOfAB : MonoBehaviour
    5. {
    6.     public Transform OtherObjectA; // First object of pair
    7.     public Transform OtherObjectB; // Second object of pair
    8.  
    9.     public void Update ()
    10.     {
    11.         Vector3 directionCtoA = OtherObjectA.position - transform.position; // directionCtoA = positionA - positionC
    12.         Vector3 directionCtoB = OtherObjectB.position - transform.position; // directionCtoB = positionB - positionC
    13.         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
    14.     }
    15. }
    Here is an example using the above code to draw debug rays showing all these vectors:
    $midpoint.jpg

    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:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HereToMidpointOfAB : MonoBehaviour
    5. {
    6.     public Transform OtherObjectA; // First object of pair
    7.     public Transform OtherObjectB; // Second object of pair
    8.  
    9.     public void Update ()
    10.     {
    11.         Vector3 directionCtoA = OtherObjectA.position - transform.position; // directionCtoA = positionA - positionC
    12.         Vector3 directionCtoB = OtherObjectB.position - transform.position; // directionCtoB = positionB - positionC
    13.         Vector3 directionAtoB = OtherObjectB.position - OtherObjectA.position; // directionAtoB = target.position - source.position
    14.         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
    15.         Debug.DrawRay(OtherObjectA.position,  directionAtoB, Color.red); // line between A and B
    16.         Debug.DrawRay(transform.position,  directionCtoA, Color.green); // line between C and A
    17.         Debug.DrawRay(transform.position,  directionCtoB, Color.green); // line between C and B
    18.         Debug.DrawRay(transform.position,  midpointAtoB, Color.blue); // line midway between C and B
    19.     }
    20. }
    And here is the full project, just open the blank scene and move the objects about.
    View attachment $MidpointBetween2Objects.unitypackage

    Any questions just ask.
     
    Last edited: Jan 10, 2014
    LukeZeches likes this.
  2. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    well done. Excellent written tutorial.

    Best,

    jon
     
  3. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    1,055
    Or to save some calculations you could just do :

    Code (csharp):
    1. Vector3 midPoint = (OtherObjectA.position + OtherObjectB.position) * 0.5f;
     
  4. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    Thanks SubZeroGaming

    MikeUpchat, I wanted to stretch it out so people could understand the maths behind it hence lines like:

    Code (csharp):
    1. Vector3 midpointAtoB = new Vector3((directionCtoA.x+directionCtoB.x)/2.0f,(directionCtoA.y+directionCtoB.y)/2.0f,(directionCtoA.z+directionCtoB.z)/2.0f);
    To show how each component of the vector is constructed.
     
  5. Alxander_Watson

    Alxander_Watson

    Joined:
    Mar 11, 2017
    Posts:
    14
    You could always lerp it:

    Code (CSharp):
    1. Vector3 mid = Vector3.Lerp(OtherObjectA.position, OtherObjectb.position, 0.5f);