0
\$\begingroup\$

I know how to find the middle of two given points:

var midpoint = (pointA + pointB)/2;

where pointA and pointB are Vector2

I thought that

var thirdBtnPoints = (pointA + pointB)/3;
var twoThirdBtnPoints = 2*(pointA + pointB)/3;

should give me 1/3 and 2/3 between the two points but it doesn't. I guess this might be more of a math question though, but how can I find 1/3 and 2/3 of the distance between two given points?

\$\endgroup\$
1
  • \$\begingroup\$ I can see only integer divisions. Try typecasting pointA and/or pointB to floating point. \$\endgroup\$
    – suvam0451
    Commented Jan 22, 2019 at 18:07

1 Answer 1

2
\$\begingroup\$

Let A and B be the two points. The formula looks like this:

desiredPoint = A +(B-A) *2/3;

Replace 2/3 by your desired fraction. This counts from point A, so 2/3 means the resulting point will be closer to point B.

The explanation is simple. You take point A, then move towards B (+(B-A)), and multiply that by how long towards B to move (*2/3).

Since you are using Unity, you can use its own function Lerp: (Thanks @DMGregory)

Vector3 desiredPoint = Vector3.Lerp(A, B, 2f/3f);

This basically gives the same result as the explanation above.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .