- Home /

# calculating the midpoint of two objects

i am trying to make an object go to the midpoint of two characters in my game and stay there but its not working, here's my script:

```
var mark : Transform;
var josh : Transform;
function Update () {
transform.position.x = josh.position.x + (mark.position.x - josh.position.x /2);
transform.position.y = josh.position.y + (mark.position.y - josh.position.y /2);
transform.position.z = josh.position.z + (mark.position.z - josh.position.z /2);
}
```

whenever i start the game the sphere just goes way off screen, does anyone know what im doing wrong?

**Answer** by cowlinator
·
Sep 20, 2012 at 03:50 AM

In programming and math, division is always evaluated before subtraction.

```
(mark.position.x - josh.position.x /2)
```

is exactly the same as

```
mark.position.x - (josh.position.x /2)
```

So instead, you should use

```
transform.position.x = josh.position.x + (mark.position.x - josh.position.x) / 2;
```

this is true !!! but i cant understand why ???

mid point between A and B is (A+B)/2

why it is A+(B-A)/2 !!!!!!

A+(B-A)/2 = A+(B/2)-(A/2) = (A-A/2)+(B/2) = (1A - (1/2)A) + (B/2) = (1/2)A + (B/2) = (A/2) + (B/2) = (A+B)/2

Right, the approach (A+B)/2 is a more "mathematical approach" as it's just the arithmetic mean.

The approach A+(B-A)/2 is more a "vectorial approach". "A" is the starting point "(B-A)" is a relative direction vector from the start (A) to the end (B). Adding that vector back to our starting point we get to the end (B). By taking half of that vector we only move half way towards B. So the resulting point is exactly halfway between A and B.

The great thing about that approach is that you're not limited to the midpoint. You could easily calculate 1/3 or 2/3 of the distance which doesn't work with the arithmetic mean.

**Answer** by DarkMatter
·
Sep 20, 2012 at 03:49 AM

The midpoint of A and B is (A+B)/2. You have A+B-A/2;

Try: transform.position = (josh.position + mark.position)/2;

**Answer** by SteenPetersen
·
Jul 28, 2017 at 11:59 PM

In my current project, my enemies need to judge the distance to 2 different vector position on the players. in order to determine if they are facing them or not. But I want them to "target" and run to a point between those two vectors. Here is how I did that:

```
[Range(0,1)]
public float myFloatVariable; // can precisely place the spot to "target" in editor.
public Vector3 drawPointOnCurrentTarget;
Vector3 a = new Vector3(GameObjectRepresentingFace.x, GameObjectRepresentingFace.y);
Vector3 b = new Vector3(GameObjectRepresentingBody.x, GameObjectRepresentingBody.y);
drawPointOnCurrentTarget = a + ((b - a).normalized * myFloatVariable);
float distanceToTarget = Vector3.Distance(drawPointOnCurrentTarget, transform.position);
Debug.DrawLine(drawPointOnCurrentTarget, transform.position, Color.red);
```

Hope this helps someone stumbling onto this question.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.