I have parent entities with different sizes, that all has a child that has to be the same size.
I think the best solution is to calculate the size for the child in the initialize based on the size of the parent and the desired size.
Anybody an idea how to do this?
Hi @Albertos ,
This thread I think continues a solution for that:
My current hierarchy looks like this
2D Screen
-ParentOne (Image Element covering left half of 2D screen)
-Child (Image Element covering left half of ParentOne)
-ParentTwo (Image Element covering right half of 2D screen)
When I call Child.reparent(ParentTwo,0); my hierarchy will change to this.
-2D Screen
-ParentOne
-ParentTwo
-Child (Now Image Element covering left half of ParentTwo)
Since the local position and local scale data is retained instead of the global data, the Child Image …
Hay @Leonidas ! I tried, but I think it’s a diffrent case. This is my setup:
Rifle (parent, scale 0.6, no script)
Item (child, scale 1.0, with item script)
Stone (parent, scale 0.15, no script)
Item (child, scale 1.0, with item script)
I’d say since the scale is always in local units, you need a reference size in world units.
You could calculate the total bounding box size for your child entity initially, and then use that to calculate the required local scale each time.
So it matches that bounding box.
1 Like
Thanks @Leonidas ! With the help of @LeXXik this solved my problem:
var currentScale = this.entity.parent.getLocalScale();
var desiredScale = new pc.Vec3(2, 2, 2);
var newScale = this.entity.getLocalScale();
var factor = desiredScale.x/currentScale.x;
newScale.scale(factor);
this.entity.setLocalScale(newScale);
2 Likes