Organising children in the local position is out of place.
So I am trying to make my game automatically organise children into a pattern within a sprite, so my structure is something like:
Sprite
child sprite
child sprite
etc, and this is the code I am using to do this:
public void organiseChildren()
{
float xPos = -spriteRenderer.bounds.size.x / 2f;
float yPos = -spriteRenderer.bounds.size.y / 2f;
float spacing = 0.5f;
foreach(Transform child in transform)
{
child.localPosition = new Vector3(0 + xPos, 0 + yPos, -1f);
if((xPos + spacing) > spriteRenderer.bounds.size.x/2f)
{
xPos = -spriteRenderer.bounds.size.x / 2;
yPos += spacing;
}
else
{
xPos += spacing;
}
}
}
However when it places the children they are all scaled out of where they need to be. Im sure this is an issue of scale but am unsure of how to remedy it.
If anybody has any suggestions, I would be very grateful :)
Thanks!
Answer by streeetwalker · Mar 30, 2020 at 11:05 AM
@Bogomip, Correct me so can understand what you are doing: the code is supposed to align rows of children within the rectangle defined by renderbounds starting at left and bottom of the renderbounds and working your way to the right and up ( assuming you are looking down on the scene and +y is up and +x is to the right).
It looks like it should work fine. If the bounds rectangle is defined by the parent, I believe the only thing that could affect your positioning is any scaling you've applied to the parent object. If so, all the positions will also need be scaled by the inverse of those respective scale amounts.
For example, assume you define the parent localScale (that's the only kind there is) on x = 0.5f, , the when you localPosition the child at x = -10f, it's actual localPosition relative to the parent will be x = -5f, because is scaled down by 0.5f.
To compensate for parent scaling, you will need to multiply all values by the inverse of the scale - same as dividing by the scale. It doesn't matter if the parent is scaled lager or smaller.
// make a shortcut, probably outside any function
private Vector3 scales = transform.localScale;
// then in your for each loop
child.localPosition = new Vector3( xPos / scales.x, yPos / scales.y, -1f / scales.z );
// note, if all parent axes are scaled by the same amount, you can just pick one and:
child.localPosition = new Vector3( xPos, yPos, -1f) / scales.y;
// P.S. no need to state '0 + xPos', and so on, unless you plan on...
// substituting zero with some value later
// Also, Bounds.extents are already = Bounds.size / 2 in each dimension,
// so you can skip the processing and just state:
float xPos = -spriteRenderer.bounds.extents.x;
If you have not scaled the parent, then I think there must be something else going on.
Answer by Bogomip · Mar 30, 2020 at 12:08 PM
Ahh, thanks! Its scaling it relative to the parent thats my issue - ok, thanks so much. I made those corrections and it was a step in the right directions :)
I should be able to move this forward myself from here, but thanks very much for the help :)