Hello, I’m trying to make a 2D game where the player builds bridges. These bridges are made by clicking and dragging from one connection point to another, while doing so the bridge is being drawn and following the mouse. The bridge is also on a pivot on its first connection, so it should rotate in the direction of the mouse and only extend its width on the opposite side.
Here’s a demonstration:
I’m having difficulty getting the gameobject (bridge) to increase its width to keep up with the position of the mouse. For instance, I’ve tried increasing the scale of the bridge but, the scale extends it in both directions (instead of just the side I’m dragging with the mouse). Would it be best to edit the far right vertices and set their position to the mouse?
don’t edit any vertices. Just adjust the localScale of the object along the X axis.
it’s that simple. have two markers (empty game objects) at 1st connection and 2nd connection
Get the distance between them using “.distance”
Say it is 4.3 meters. Say the natural length of your blue object is 2.7 meters.
Simply set the scale on X of the blue object to (4.3/2.7)
if you have trouble using .distance or scale, simply search on here for 100s of answers about them, or ask a separate question
As I mentioned below typically you let the software get the original length. Fortunately this is very easy in unity. I opened the first project I saw in front of me and searched on “size” and found dozens of examples!
lengthNow = ourSprite.renderer.bounds.size.x;
transform.localScale.x = ( desiredLength / lengthNow );
transform.localPosition.x += 0.5 * desiredLength;
Note that it’s also moving it so the center stays in the correct position!
one point – the incredibly useful RotateAround function is often useful with this. Cheers