- Home /
How can I scale my object in one direction???
Hey Everyone,
I've searched a lot but couldn't find what I want...
when I drag my mouse left for scaling my object but the object become wider from both sides and I only want it in on direction...
Is there any sample way to scale the object on one direction???
Answer by YourGamesBeOver · Mar 05, 2014 at 10:47 PM
Try this: make an empty GameObject, and add the one you want to scale as a child of it. move the inner object to position 0,0,0. then move the inner object such that the center of the parent object is at the edge of the inner object (in your case, the right edge). Now resize the outer GameObject as needed. Before you try to apply this, reset the scaling and position of your object, that should make this easier.
I made an empty object and added my object to the empty object but still no luck
This is the answer made by @YourGamesBeOver . But I broke them into steps and made few corrections.
Create an empty game object and reset the position,scale
$$anonymous$$ove the game object you would like to scale , such that the center of empty game object is at the edge of the game object you want scale.
Now put this game object as the child of this empty game object. Now try to scale the empty game object, it would scale in one direction .
Answer by thedetective456 · Dec 30, 2020 at 06:23 PM
Hi everyone!
For anyone wondering how to scale an object in a specific direction, this little function maybe helpful.
public void Resize(float amount, Vector3 direction)
{
transform.position += direction * amount / 2; // Move the object in the direction of scaling, so that the corner on ther side stays in place
transform.localScale += direction * amount; // Scale object in the specified direction
}
Usage : If you want to scale a platform in z direction by 5 units, then
Resize(5f, new Vector3(0f, 0f, 1f); // You can use Vector3.forward instead
That's it. Enjoy, Have a nice day!
Answer by Asle_Kinnerod · Mar 05, 2014 at 09:06 PM
You should change your pivot to FI "top-left". Depends on what kind of object, but normally pivot is set to center as default.
What kind of object are you working with? I´m not raven sure that you have the possibility on all kinds of objects.
Im working with a cube how can i change its pivot to left??
Answer by scintilla0 · Jan 21 at 08:12 AM
If anyone needs to revert object to the old size here:
void Update()
{
if (Input.GetMouseButtonDown(0))//LeftMouseButton
{
IncreaseObjectSize(5f, new Vector3(0f, 0f, 1f));
}
else if (Input.GetMouseButtonDown(1))//RightMouseButton
{
DecreaseObjectSize(5f, new Vector3(0f, 0f, 1f));
}
}
public void IncreaseObjectSize(float amount, Vector3 direction)
{
transform.position += direction * amount / 2; // Move the object in the direction of scaling, so that the corner on ther side stays in place
transform.localScale += direction * amount; // Scale object in the specified direction
}
public void DecreaseObjectSize(float amount, Vector3 direction)
{
transform.position -= direction * amount / 2;
transform.localScale -= direction * amount;
}