- Home /
Find Centre Point of 2 Vectors
Hello,
I want to create a chain type effect, so when I click and drag from my scene, spheres will line them selves up from where I initially clicked, to where I have dragged to.
I've achieved this by using three spheres:
One for the starting marker (so where I initially clicked)
One for the ending marker (so where my mouse position is currently)
And a sphere for the middle point.
The problem is now, is that I want to have multiple middle points. So instead of 1 middle sphere, I want to have 3. And I want them to line up perfectly between the starting marker and the finishing marker:
if(Input.GetButton("Fire1")){
isDragging = true;
end.position = hit.point;
//middle.position = (end.position / 2) + (start.position / 2);
for(var m = 0; m < middles.length; m++){
middles[m].position = ((end.position * m / 2) + (start.position * m / 2));
}
}
Thanks
Answer by whydoidoit · Sep 26, 2013 at 12:57 PM
Well just take one position from the other, then multiply that difference by a number between 0 and 1 to get that interval and add on the lower value.
var difference = start - end;
var quarterPoint = start + difference * 0.25f;
var midPoint = start + difference * 0.5f;
Okay, but what about if I want to make it dynamic? Eg, the further I drag away, the more spheres are initiated - I will then need to calculate them in too
var numberOfSpheres = difference.magnitude / 2;
for(var i = 0f; i < 1f; i += 1f/numberOfSpheres)
{
var spherePos = start + difference * i;
}
So I've done this:
if(Input.GetButton("Fire1")){
isDragging = true;
end.position = hit.point;
var difference = start.position - end.position;
var numberOfSpheres = difference.magnitude / 2;
for(var i = 0; i < numberOfSpheres; i += 1f/numberOfSpheres){
var spherePos = start.position + difference * i;
middles[i].position = spherePos;
}
}
And it crashes every time I run it :/
Actually just updated my answer as I screwed up the inc of i
so you want:
if(Input.GetButton("Fire1")){
isDragging = true;
end.position = hit.point;
var difference = start.position - end.position;
var numberOfSpheres = difference.magnitude / 2;
for(var i = 0; i < numberOfSpheres; i++){
var spherePos = start.position + difference * ((float)i/numberOfSpheres);
middles[i].position = spherePos;
}
}
Oh and you need a $$anonymous$$athf.FloorToInt on the number of sphere setting:
var numberOfSpheres = $$anonymous$$athf.FloorToInt(difference.magnitude/2);
Your answer
![](https://koobas.hobune.stream/wayback/20220613114840im_/https://answers.unity.com/themes/thub/images/avi.jpg)