[Solved]Creating an object bewteen two others.

Hi,

I’d like to create an object between two others. This object must have its size depending on the position of the two others. It’s basically a bridge : it’s created from the two edges of my left and right platforms.

I’ve tried with localscale but i have no idea how to make it depend on the positions of the other game objects.
If I specify the localscale manually I can get the desired size, but I want it to be automatic, and I can’t get it from the space between my two platforms.

Thanks in advance :slight_smile:

You need the width of the bridge object before scaling, and then you divide the distance between the objects (or inner edges) by that width to get the amount to scale the bridge object.

Then depending on the pivot of the bridge you must position it. If it’s a left-pivot then you set it to the left platform’s position, if middle-pivot then set it at the midpoint between the platforms, right-pivot then set it to right platform’s position.

I forgot to mention that I don’t have the width of the bridge; it needs to be calculated from the space between my 2 platforms.

float width = newBridge.GetComponent<Transform> ().localScale.x;
float amount = width / (clou2.GetComponent<Transform> ().position.x - clou1.GetComponent<Transform> ().position.x) ;
newBridge.GetComponent<Transform> ().localScale += new Vector3 (amount, 10, 0);

clou1 and clou2 are my two platforms’ edges.

Whatever sprite you’re stretching, you need to know the world space width of that to start.

I have the size of the space
But when i apply it to the localscale of my bridge it is to big.

float space = clou2.GetComponent<Transform> ().position.x - clou1.GetComponent<Transform> ().position.x;

Thanks for your answer ! :slight_smile:

Please re-read my responses. You’re not understanding what I’m saying.

If your bridge sprite is 50 pixels wide, and your PixelsPerUnit for that image is set to the default 100, then your bridge is 0.5 world-units wide.

That is your bridge’s width before scaling.

Then take the distance between the platforms, and divide by the bridge’s width.

If your platforms are 100 units apart, that would be:

(distance) / (bridge width) = scalar for bridge

100 / 0.5 = 50

So in this example, if you set the bridge’s X scale to 50, it will fill the gap.

1 Like

You’re right, i didn’t get what you were saying!
I just forgot that my sprite had a base width…
It works perfectly, thank you a lot!! :slight_smile:

1 Like