- Home /
Question by
zack_falcon · Aug 15, 2016 at 03:34 PM ·
positioningdimensions
How can I get the width of a gameobject?
I'm a noob in Unity, trying to make a tower of hanoi style game.
I'm trying to make sure a gameobject (2D sprite, say, panda) stays to the left of another (say, bamboo), regardless of the former's size. I thought I could take the bamboo's location, and deduct the width of the panda from it.
Something like this:
Here's my code so far:
foreach (GameObject panda in midBambooStack)
{
Vector3 newPosition = new Vector3(midBamboo.transform.position.x - panda.transform.lossyScale.x, panda.transform.position.y, panda.transform.position.z);
panda.transform.position = newPosition;
}
Unfortunately, whenever that code runs, here's what I get:
Any suggestions for a code that does the former pic? Thanks.
capture.png
(241.5 kB)
capture2.png
(150.6 kB)
Comment
Your panda is a sprite or a ui image ?
As for your logic note that the position.x of the bamboo only represents the center of it, so you have to account the bamboo width/2 on your calculation too.