0
\$\begingroup\$

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:

enter image description here

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;
}

But what I end up is that the panda goes right in the middle of the bamboo, so I'm assuming the lossyScale.x bit is wrong.

enter image description here

Any suggestions?

EDIT: Here's my hierarchy, if it means something.

enter image description here

\$\endgroup\$
  • \$\begingroup\$ is there any rotation on the pandas? \$\endgroup\$ – Gnemlock Aug 14 '16 at 8:34
  • \$\begingroup\$ @Timelord64, nope, I disabled them, because I had issues with dragging and dropping and rigidbodies. \$\endgroup\$ – zack_falcon Aug 14 '16 at 8:37
  • \$\begingroup\$ are you decreasing or increasing scale? is there are any parent-child or the sprite is root? \$\endgroup\$ – Sourav Paul Aug 14 '16 at 9:03
  • \$\begingroup\$ @SP. Scale? I'm sorry, I don't think I understand your question. Each of those pandas are a different gameobject. I updated the question with a pic of my hierarchy. \$\endgroup\$ – zack_falcon Aug 14 '16 at 9:23
  • \$\begingroup\$ what happens if you just use regular scale? Transform.lossyScale specifically gets you the global scale, but in your case, the pandas are not children of any other object. Since they do not inherit a scale from any other object, you should be fine with regular scale. I believe @SP means did you load in the smallest panda, and scale it up to the larger sizes, or load in the largest panda, and scale it down to the smaller sizes. \$\endgroup\$ – Gnemlock Aug 14 '16 at 12:08
3
\$\begingroup\$

This will rely on the Pixel Per Unit value of your sprite. If you do not have a 1:1 ratio, then a value of "1" for your LocalScale.x will not be the same width as your sprite.

Either set the sprite's Pixel Per Unit value to a 1:1 ratio, or manually calculate the ratio multiplier:

float PixelUnits;

private void CalculatePixelUnits()
{
    PixelUnits = Sprite.rect.width / Sprite.bounds.size.x;
}

In the above code you would pass in your Panda sprite.

With this value you should then be able to position these Pandas properly:

foreach (GameObject panda in midBambooStack)
{
    Vector3 newPosition = new Vector3(midBamboo.transform.position.x - panda.transform.localScale.x * PixelUnits, panda.transform.position.y, panda.transform.position.z);
    panda.transform.position = newPosition;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.