Resizing a gameobject based on mouse position

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:

alt text

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?

1 Answer

1

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!

var lengthNow:float;
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

Thanks, that works!

great! it's a "standard trick" here's something that may help. as you know in programming you try to generalize everything, to make it easier next time. You see where I said "the original length is 27 meters" in fact. With one line of code, your software can FIND the length of the thing. Rather than you having to measure it and hard-code it in. what you usually do is simply use the renderer, and get the size of it! it's great - one line of code. Indeed you see "renderer.bounds. ..." everywhere in video game code. I'll post in an example.

ive seen this but it doesnt work for me, im using this line ArrowPrefab.transform.localScale.x = dragDistance; but im getting the same error Cannot modify a value type return value of `UnityEngine.Transform.localScale'. Consider storing the value in a temporary variable but im pretty sure dragDistance is already a variable or am i wrong? this is the full script http://hostcode.sourceforge.net/view/1023

lem, are you using c# or U/S ? if c#, you just need to set the whole vector at the same time. see 100000s of examples on here

@lemiwinks, I have seen that error a million times. If I´m not wrong, you can´t assign the x of a localScale vector directly, you have to assign a whole vector. Instead of: ArrowPrefab.transform.localScale.x = dist; use this: Vector3 temp = ArrowPrefab.transform.localScale; temp.x = dist; ArrowPrefab.transform.localScale = temp; There are also other vectors which you have to change this way (storing data in a vector) but I don´t remember them right now. Anytime you see the error you described, this is likely to be the cause.