#Can I ask for some help about position children within a parent?

1 messages · Page 1 of 1 (latest)

blazing anchor
#

I'm a little lost. I need to generate 16 cubes in two rows and 8 columns within an existing game object. I want the top row to always start at the top of the existing game object and the bottom row to always rest on the bottom. The heights of the 16 cubes will be random but each column will always total 100% of the existing game object.

So, I don't understand the units properly on how to do this. My first attempt involved using localScale and localPosition but its not working at all. Can I please ask for recommendations on how to do this most easily? Here is my current code and I will attach a screenshot of what it produces.

peak island
#

possibly could be made easier if the pivots were set on the end of the cubes

blazing anchor
#

So, I tried to research that didn't have the keyword pivot. Does that mean I can stop having to base everything off the center?

#

I think my main issue is I don't understand what a unit represents inside of a parent object

#

Is it the same as global space?

peak island
#

if the scale is 1,1,1, yeah

#

it's not normalized

blazing anchor
#

The scale currently on the parent object is 1, .45, .3

#

Based on my readings I can't change the pivot

peak island
#

you could make an empty to wrap the cube, making that empty essentially the pivot

peak island
blazing anchor
#

that is very confusing...lol

#

if I just use position instead of localPosition can I work with normal units?

#

I assume its the exact same thing with scale too right? If I use localScale is ratio that is based off the scale of the parent? But if I just use transform.scale, I can keep using the global units?

peak island
blazing anchor
#

Ok, that makes sense.

peak island
#

one thing you could do would be doing this all assuming the parent is 1,1,1 and then treat the non-1,1,1 scale as just making the parent look nice

blazing anchor
#

I'm sorry for being dense. I think I get the scale vs localScale now. For localPosition, what does 1,1,1 mean?

#

It doesn't mean back right corner

peak island
#

i'm talking about the scale there

#

again, it isn't normalized

#

positions are just an offset

blazing anchor
#

My parent isn't 1,1,1

#

So I don't know how that helps

peak island
#

a root object's position is an offset from the origin

blazing anchor
#

Ok, but the position distance per unit doesn't change based on if its a child or not?

peak island
peak island
blazing anchor
#

oh no

#

I just want to space these out inside an element

peak island
#

i feel like you're overthinking this a bit

blazing anchor
#

It seems like using the localPosition and localScale just makes things 100x harder

peak island
#

all the inner cubes' pivots are at their centers, right?

blazing anchor
#

AFAIK there is no way to change that

peak island
blazing anchor
#

If I just get the parent position and then work off that and use position + scale (not local) for all the cubes it seems like the easiest thing

peak island
#

are these specific meshes' pivots at their centers

blazing anchor
#

I'm just doing : GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);

peak island
#

cool, so the built-in one

#

are the cubes' sizes 1,1,1 when scaled 1,1,1?

#

hmm that's probably documented, huh

#

alright yeah it is that size

blazing anchor
#

I couldn't find any info on that command, the docs are useless

peak island
#

CreatePrimitive links directly to the manual page with info about primitives

blazing anchor
#

Ok, that looks better

peak island
#

you really don't need the row == 0 checks there

#

you can just swap the row and column loops

#

it kinda seems like the issues stem from accounting for containerScale

blazing anchor
#

I don't see how its possible to use localScale or localPosition without introducing a ton of extra math

peak island
#

you definitely don't need more than what you have

#

might be less actually

blazing anchor
#

Maybe I'm missing something. I just don't see any benefit of having a new scale that I have to work with. My head wants percentages

peak island
#

because you don't have to work with it

#

you can treat scale as percentage, the normal size of the cubes is 1x1x1

#
margin = 0.02
split = [0.8f, 0.5f, 0.3f, 0.7f, 0.1f, 0.4f, 0.6f, 0.2f]
columns = split.length
width = 1f / columns - (1 - margin)

for col 0..columns:
  xPos = (col + 0.5) / columns - 0.5

  topSplit = split[col]
  topCube = create cube
  topCube.localPos = { xPos, 0.5 - topSplit / 2, 0 }
  topCube.localScale = { width, topSplit * (1 - margin), 1 }

  bottomSplit = 1 - topSplit
  bottomCube = create cube
  bottomCube.localPos = { xPos, bottomSplit / 2 - 0.5, 0 }
  bottomCube.localScale = { width, bottomSplit * (1 - margin), 1 }
#

i think this is all you need

#

you don't need to account for the container's scale

#

the transform does that

blazing anchor
#

Wow..let me try to get this to run. Thanks a lot.

#

I understand better how it all works now at least, that was my main goal.

#

Thanks for your help. I appreciate it @peak island

peak island
#

ahh im just now realizing i forgot to put in the parenting

#

oh well

#

it would be done right after the create calls

blazing anchor
#

No worries.

#

If your still around....why adding/subtracting .5 for xPos?

#

oh...its causes its based off the center

#

I keep forgetting that

#

yeah...none of this makes sense to me...shouldn't yo be multiplying .5?

peak island
#

for xPos?

blazing anchor
#

all the positions really

peak island
#

consider: multiplying is scaling, adding is an offset

blazing anchor
#

oh...and we know the cube is 1x1x1

peak island
#

with x positioning, there are 2 offsets
the + 0.5 applied to col is for the pivot of the child being in the center - the targetted position is a little offset
the - 0.5 at the end is for the pivot of the parent being in the center - the targets are all offset by half the parent (and the full size of the parent is just 1 - don't need to consider the scale, that's applied to both sides of the equation)

blazing anchor
#

I'm translated something wrong 😭

peak island
#

you really don't need to loop over the 2 rows here man

blazing anchor
#

it prevents all the dup cube creation code

peak island
#

eh sure

#

could you send it in a pastesite

#

!code

lime oasisBOT
blazing anchor
peak island
#

i am dumb, this

width = 1f / columns - (1 - margin)
```should be this```cs
width = 1f / columns * (1 - margin)
blazing anchor
#

BoxCollider does not support negative scale or size.
The effective box size has been forced positive and is likely to give unexpected collision geometry.
If you absolutely need to use negative scaling you can use the convex MeshCollider. Scene hierarchy path "Enviroment/LoadingCubes/Cube_1_2"

#

Definitely looking a lot better

peak island
#

line 55

height = ((1 - cubeSplit[col]) / 2f) - 0.5f;

this needs to be cubeSplit[col] * (1 - margin)

#

looks like you copied the yPos there lol

blazing anchor
#

No change with that update

peak island
#

make sure you saved and restarted

#

those are very different values

blazing anchor
#

its also line 56

peak island
#

shrugsinjapanese line 55 in the paste you sent lol

blazing anchor
#

weird...I didn't realize I added a line

#

code is now :
height = cubeSplit[col] * (1 - margin);

and no change to output

peak island
#

there definitely should be a change

#

those are numerically very different

#

well not that different but significantly different

blazing anchor
#

ok...its a little different actually

peak island
#

well no more warnings, that's a good change

#

ah wait, it should be (1 - cubeSplit[col]) * (1 - margin)

#

i was looking at my pseudocode with the bottomSplit lol

blazing anchor
#

that looks really good

#

seems like xpos is the only thing left

peak island
#

also xPos is wrong

float xPos = (col + .5f) / (columns - .5f);

the - .5f needs to be outside parens, at the very end
xPos = (col + 0.5) / columns - 0.5

blazing anchor
#

awww.....its so perfect

#

seriously, just what I wanted

#

and the code is really clean

peak island
#

cool

blazing anchor
#

I really appreciate it

#

Now I've got something I can reference going forward

peak island
#

(you now have a few unused variables from the container btw)

blazing anchor
#

I see it up top

#

I'll pull those

peak island
#

are there any parts you want explanation/clarification on?

blazing anchor
#

Hmmm...so the top actually isn't quite level

peak island
#

(you can ask later of course, im just going soon so that would have to wait)

peak island
# blazing anchor

ahh shoot the margins are applied by percentage, i forgot to account for that in the height

blazing anchor
#

I'm willing to switch to a fixed amount

peak island
#

it could still be a percentage, just a percentage of something else instead

#

like the full range perhaps

#

what about x * (1 - margin) -> x - margin for height?

#

since the parent container's x/y scale isn't square, it'd be a bit more math to make the outside border nice

blazing anchor
#

x is xPos?

#

I don't know what full range means

#

or is x = cubeSplit[col]

peak island
#

i mean x just as [something]

#

so like x would be cubeSplit[col] in the row == 0 case

peak island
peak island
blazing anchor
#

pretty sure that is what I currently have

#

height = (cubeSplit[col] * (1 - margin));

#

ok just margin...not 1 -

peak island
#

and turn it into subtraction, instead of multiplication

#
- height = (cubeSplit[col] * (1 - margin));
+ height = (cubeSplit[col] - margin);
blazing anchor
#

Looks good to me

peak island
#

hm a bit big compared to the vertical gaps

blazing anchor
#

I don't mind that

peak island
#

you could split those variables and tweak them separately pretty easy if you want to

blazing anchor
#

yeah, no problem

#

so if you havea second...how is setting height to .8f - margin working to fill the area?

#

When my y scale on the container object is not 1

#

I thought it took the scale of the parent

#

So my container object that I'm shoving all these in is x=1, y=.45 and Z=.3 scale

#

For children, is 1 the full height?

#

of the container object?

peak island
#

yeah, the parent's scale is applied to both children position and scale

blazing anchor
#

I thought 1 would be (.45/1)

peak island
#

it affects the local space within the parent

blazing anchor
#

Sorry those words don't mean anything to me.

#

It 1 localScale.y unit is the height of the container object?

peak island
#

yes, but also that's partially because the cube's size is 1x1x1

#

the space isn't normalized, but the cube is

blazing anchor
#

If its not too much, can we just focus on y scale and have an example where the parent object is 1.76 or something

#

What is the formula to determine what 1 localScale.y unit would be inside a parent object with a mesh y size of 1.76 might be the right way to say it?

peak island
# blazing anchor Sorry those words don't mean anything to me.

imagine the world as just a big object with space inside
each "root" object is a child of the world, with its own local space
if an object is moved, that moves the local space
if an object is rotated, that rotates the local space
if an object is scaled that scales the local space
any child of that object respects that local space - stuff based on worldspace actually has to go through some math to figure out all the parents

blazing anchor
#

ok...I can picture that in my head and understand why it would be designed like that

peak island
#

if you have a child cube at position 0,0,0, then it's the same position as the parent, regardless of the parent's position
if you have a child cube with rotation 0,0,0, then it's the same orientation as the parent, regardless of the parent's rotation
if you have a child cube of scale 1,1,1, then it's the same size as the parent, regardless of the parent's scale

#

so a child cube of scale 0.5, 0.5, 0.5 is half the size of the parent

#

a child cube of scale 0.2, 0.2, 0.2 is 20% the size of the parent

blazing anchor
#

ok...so it is all precentage based?

peak island
#

well, technically not percentage

#

it's all multiplied together (in the case of scale)

blazing anchor
#

all the parents above it you mean

peak island
#

yep

#

if you have a cube of scale 0.8, and then a child of scale 0.5, the true/global size of the child is 0.8 * 0.5 = 0.4

blazing anchor
#

ok, but the child scale of .5 is always half the size of the cube

peak island
#

(like how local positions are all added together, or how local rotations are all compounded by quaternion multiplication together. let's not get into compounding the transformations)

blazing anchor
#

haha...agreed

#

but no matter what, 1 localScale.y of a child is always equal to the full height of the parent correct?

peak island
#

if they're the same size, yeah

blazing anchor
#

this is the mesh size correct?

peak island
#

sure, but also visually - there's not really a "size" baked into the transform/math

#

just, anything you can identify as the "size"

blazing anchor
#

haha...sorry...I have no idea what that means

#

but I think I understand enough to figure it out next time I'm dealing with this

peak island
#

what im saying is the transform math doesn't have a concept of "size"

#

anything you can call a "size", it will respect the transform

blazing anchor
#

I understand the compound nature of these units and I understand its based of the starting size of the mesh

#

Anyway...thanks a million for all your time with these silly questions.

#

I've got a lot to learn and you helped me along my path

#

what I got right now looks amazing

sleek pier
# blazing anchor

just a tip - you should set your tool handle position to "Pivot" instead of "Center"

#

it will make things with parent/child positioning make way more sense

blazing anchor
#

@sleek pier : So I tried forever to learn about this and couldn't find anything

#

How do I do that?

sleek pier
blazing anchor
#

I literally searched pivot a few hours ago when I learned the word above and found nothing : e08d38

sleek pier
#

Press Z as a shortcut - or switch this dropdown to say Pivot instead of Center

blazing anchor
#

Hmmm...so Z doesn't seem to do anything when I have a cube selected in the editor and I can't find that dropdown anywhere in the inspector

#

@sleek pier

sleek pier
#

it's in the top left of scene view

#

i took that screenshot from YOUR screenshot

sleek pier
#

the cubes have no children so it will make no difference for them

blazing anchor
#

Ok. So its basically for animation it seems? It's not going to help in this situation?

sleek pier
#

it's for knowing where your obejcts actually are

#

And making sense of local positions

blazing anchor
#

I think I found a good youtube video about it. I dont' really get why there would be a requirement to have children. I need to set the pivot on these cubes...seems pretty basic.

#

Ok...that tutorial is too old apparently

sleek pier
#

i'm just telling you how the pivot tool handle behavior works

#

When you have Center mode on - the position of the tool handle (the position gizmo for example) will show at the average of the positions of all of the objects' child renderers

#

this is nice sometimes but it can lead to confusion about the actual position of your object (the one you will get in code with transform.position)

#

with Pivot mode on, you always see the actual world space position of the object.

blazing anchor
#

@sleek pier I tried to find some videos on how to use the pivot functionality and couldnt find anything. Changing the drop-down doesn't seem to do anything in my editor.

peak island
#

the pivot and the center of the cubes are the same position

#

that's not necessarily true for all objects

#

try this - while in play mode, when you've created the loading bars, pause and move one of the child cubes a few units away
then select the parent cube with a tool that shows the origin (ie, translate) and try changing the pivot/center mode