Hello! I have object A, how can I find out its position between objects B and C, and I also somehow need this distance to be counted from 0 to 1. Is this possible?

Hello! I have object A, how can I find out its position between objects B and C, and I also somehow need this distance to be counted from 0 to 1. Is this possible?

You can use Vector3.Distance to get a distance between two objects.
But how exactly you define your “between B and C”?
It already looks like your object isn’t located in a line between B and C, so you probably need to explain this.
“I also somehow need this distance to be counted from 0 to 1”
If you need a normalized value, you can get it by dividing your current distance value by total distance. 3 units / 10 total units will give you 0.3.
Vector3.Distance won’t work well if as seen in your image object A is not really between B and C but offset sideways. You generally want to project the vector from B to A onto the vector from B to C
So the general approach would be
Vector3 BA = A - B;
Vector3 BC = C - B;
float distSqr = BC.sqrMagnitude;
float d = Vector3.Dot(BA, BC) / distSqr;
The resulting value “d” will be a value between 0 and 1 if the projection of A is between B and C. The value will be smaller than 0 if A is “before” B and will be larger than 1 if A is “behind” C. So it just linearly continues. If you need a value that is clamped between 0 and 1 you can use Mathf.Clamp01.
That is neat. I’ve only been able to use Vector3.Project and this one has benefits compared to it.
OP - if I were you, I’d use Bunny’s solution.
Well, techincally Vector3.Project does pretty much the same thing but in the end multiplies that value by the incoming “normal” vector in order to return the projected vector. Since we are only interested in that “scaling value” we don’t need this multiplication.
“Well, techincally Vector3.Project does pretty much the same thing”
I’ll have to underline my math understanding is really weak… I’ve usually simply used Vector3.Project because it exists. Then I’ve compared the resulting vector to another vector. I hadn’t figured any way to get it work exactly like your code, but whatever I got working close enough, was OK. It was probably something like this:
(OP - don’t use this…)
d = Vector3.Project(BA, BC).magnitude / BC.magnitude;
As you can see, it isn’t as versatile as your solution (doesn’t work for negative values). I didn’t know one could use dot product like this, I’ve only use it with normalized vectors for detecting directions. So I’ll keep your solution in my notes and use it from now on ![]()
A cheap version of Bunny’s idea should work. Find the distance between the markers (A and B), then the distances from C to A and B. If both are close to half, then C’s about in the middle. In the picture, suppose AB is 10, while AB and BC are 5.5 – both about half of 10. But if C were on the other side of A, perfectly 5 from it, that’s a whopping 15 from B, so no good.
The projection trick is better. It has you transform it to pretend A is 000 and A to B is the z axis. Then C.z should be about 1/2 of B.z, and C.x and C.y should be small (0 means exactly on the line). That’s an “easy” use of local coordinates. Easy as in if you know the trick. Ummm… (from memory, 20% chance of being corect as-is):
Quaternion toABlocal=Quaternin.LookRotation(B-A).Inverse();
Vector3 B2=toABlocal*(B-A);
// B2 is now (00z) where z is the distance from A
Vector3 C2=toANlocal*(C-A);
// if C2 were perfectly between it would be (0,0,B2.z/2)