- Home /
Is there a way of correctly parenting to get scales and positions of gameObjects right?
Originally, I was doing this:
void Update ()
{
this.transform.position = new Vector3 (previousObject.transform.position.x + prevRend.bounds.size.x / 2 + rend.bounds.size.x / 2, this.transform.position.y, this.transform.position.z);
//the position of both gameObjects should be aligned when one or both of their scales change
}
But because gameObjects didn´t update their positions fast enough (caused gaps for small amounts of time), I thought maybe making something different. So I commented the rest of my doings earlier, which is basically changing the scale of a parent object based on a percentage to make its children do the same. Their positions changed automatically, avoiding the gaps that I had. Also this works with the correct proportions for every gameObject and even the positions are right... when including everything. I wanted to increase or decrease size of some of this gameObjects independently to the others, but when these are out of the parent the positions are not aligned anymore. Is there some way of correctly parenting them and get both things right?
previousObject.transform.position.x + prevRend.bounds.size.x / 2 + rend.bounds.size.x / 2
Can you expand a little on what you're trying to achieve? There isn't a speed or anything here, and we don't know what those other objects are.
Ok, so this gameObjects are like pieces of the ground that I want to keep together when their scale changes. There is no speed because this change in position should be immediate, like the change in position from a parent relation.
If you want to scale pieces of the ground up and move them? Why don't you just change the scale of the parent object and move its position?
If you change the scaling of the parent you automatically scale the children.
So apparently, if a parent object has the percentage scale script and has a lot of children with different values, each percentage applies correctly. However, there are some gameObjects that I want to keep the same value until something happens. If those objects are also children, the position does what I want. But because I didn´t wanted them to increase or decrease size like the others, I removed them from the parent. So again, everything has the correct scale, but then their positions are not aligned. Is there some correct way of parenting them and get both things right?
Answer by hexagonius · Oct 07, 2018 at 12:37 PM
there are two ways of doing this:
- keep your objects children and by the time they should be locked to their scale, save the parent scale and keep multiplying the local scale by saved scale divided by current parent scale, which will counter scale the object and therefore keep it's size (works with just one parent that's changing scale only)
- unparent the children by the time they should be locked to their scale, but save their local position beforehand. now you can use parent.TransformPosition(savedLocalPosition) to get the scaled position you can set the childs position to. since not parented, it will keep it's size but move relative to the parent through scale.
I recommend the second option
Thanks, but I think I don´t understand what you are telling me to do. I don´t get how works the first option, so I tried the second option and added this script to each constant scale object:
Vector3 savedPosition;
public GameObject parent;
Vector3 scaledPosition;
public GameObject child;
void Awake ()
{
savedPosition = this.transform.position;
}
void Update ()
{
scaledPosition = parent.transform.TransformPoint(savedPosition);
//I used transform.TransformPoint since the TransformPosition wasn´t in the scripting reference
child.transform.position = scaledPosition;
}
So as I was saying, I added this script to every base (the constant scale objects) and set the child variable to only the child gameobject next to this one, which didn´t actually cover all the children (since there were 3 bases and 4 children). It didn´t work.
Answer by Feref2 · Oct 08, 2018 at 06:09 PM
OK, so actually a better solution was just to use InvokeRepeating () to update the position faster avoiding gaps. Nothing related to parenting.