How to change the Anchor of GameObject when scale it? if i want to scale it from left to right. like ---------->|,
not ----------->|<-------------
I’m waitting…
Make the object a child of a parent object; scale the parent instead.
Thank you man, i try to do it follow you , But the Anchor point always is center when i create new Object for it. Could give me some detail info ??
Just to make sure I understand…
if you have a 2d box. Right now, when you scale it, all corners expand away from the center (anchor) as you scale it. You don’t want this.
Instead, let’s say you want the “anchor” to be the bottom left corner. When you scale it, the bottom left corner stays at the anchor. The other corners expand away from the bottom left corner. Is that correct?
If so…the only way I can think of to do this in Unity is to actually define the anchor yourself, and actually move each vertex of the box/cube/whatever away from the anchor. I’ve been using Unity for a while now, and I’m not aware off the top of my head if Unity has a “Scale from position” function.
What you might do is, attach an empty gameobject to your original object (Object A), and call it Anchor. Position it where you want it to be. Or you can just add a script vector variable, that holds the anchor’s local position.
Add a script to object A, your original. Name it something like Scaler.
In the Startup function of the script, find the “Anchor” child gameobject. Save it’s position locally relative to object A’s position, and save that in a field variable. So, if your object is a cube, and the anchor is at the bottom left front corner (um (-1, -1 -1) I believe).
Also get a reference to the mesh on object A, and save that too.
Then, create a Scale function, that accepts an amount.
In the scale function, you will look through all the vertices in the mesh. You’ll take subtract the saved anchor position from each vertex position, and multiply that value by your scale. Add it to the anchor position and that is your new vertex position. Save the updated vertices back to your mesh.
From then on, just get the Scaler component attached to your objects, and call the Scale function passing in the scale.
I guess
I don’t have Unity in front of me, but I think that’s the general idea. I probably got some things backwards, but I hope it gets you the general idea.
Maybe an extension method would be useful here, on the Mesh. And an editor gizmo eh?
GameObject → Create Empty
GameObject → Create Other… → Cube
Drag “Cube” onto “GameObject” (a triangle appears by “GameObject”)
Click “Cube”
Move the cube
Click “GameObject”
Change the scale of “GameObject”
Position the empty parent object at the anchor point you want to scale from.
Thanks every dude…