- Home /
How to scale object in Runtime, without loosing position
I'm trying change width of wall elements during runtime.
if (Input.GetMouseButtonDown(0)) {
transform.localScale += new Vector3(1, 0, 0);
transform.localPosition += new Vector3(-0.5f, 0, 0);
}
if (Input.GetMouseButtonDown(1)) {
transform.localScale -= new Vector3(1, 0, 0);
transform.localPosition -= new Vector3(-0.5f, 0, 0);
}
It works fine, when I have no rotation. Otherwise the length changed correctly, but the origin position moves somehow depending actual rotation...
Any idea how to correct position regarding rotation ??
Any particular reason, why you don't have the object in the editor so that the object's pivot is where you want to scale from/towards? That way you don't have to touch the position at all.. You might need to move the mesh in whatever tool you use to create your mesh. Or you need to change some parenting of your objects..
Because my customers then need Unity and also have access to all other sources. In editor it is quit easy, but I have to make it in Runtime...
Answer by Xarbrough · Jul 01, 2015 at 11:09 PM
If you can't change the imported models pivot, you can still parent the object under an empty gameobject and then offset in a way that the empty acts as the new pivot. You can then rotate the empty pivot object. This is easiest in the editor but you can still build it in code.
Anyway it doesn't sound like a good solution, whoever made the model should be able to give you a version that has the correct pivot.
Tried this before, but because localScale changed in both directions I still have to change localPosition, and then I have the same problem. There must be a transform function calculation regarding actual object rotation... Correction : I didn't reset elements under parents to 0, It works fine now !! Thank you...
Your answer
![](https://koobas.hobune.stream/wayback/20220613201613im_/https://answers.unity.com/themes/thub/images/avi.jpg)