- Home /
How to preserve size of children when changing scale of parent
Hi all
Got a bit of a conundrum. I have some scaled objects which I would like to be able to shrink, revealing within them other objects. Consider it a rock with gems inside I want to shrink the rock to show the gems.
After having a bit of a read through these pages it seemed the simplest thing to do in order to preserve the scale of the gems would be to un-parent the gems, change the scale of the rock, then re-parent the gems. Something like this.
void NewRockSize(float Size)
{
gem.transform.parent=null;
rock.transform.localscale=new Vector3(Size,Size,Size);
gem.transform.parent=rock.transform;
}
In direct contradiction to the doc page Here the scale of the gem is not recalculated and, as it inherits the scale of its parent rock it appears the wrong size.
Answer me these questions three...
Am I doing something wrong ?
Are the Docs wrong ?
Is there a known way to do this right ?
Answer by Jashengmato · Mar 23, 2013 at 06:23 PM
Have you solved this problem yet? If not, try this.
float rockOriginalScale = rock.transform.localScale.x ;
void Resize(float size)
{
float scale = rock.transform.localScale.x / size ;
float gemOrg = gem.transform.localScale.x ;
rock.transform.localScale = new Vector3(size, size, size) ;
gem.transform.localScale = new Vector3(gemOrg*scale, gemOrg*scale, gemOrg*scale) ;
}
This should work!
Answer by Rispat-Momit · Sep 07, 2018 at 08:23 PM
Hi there! I figure out this solution :D
Just set this script to your child ;) It also works in Edit Mode so that you can set up the scale you need.
At the FixeScale set up the size of your child you need and at the parent set your parent model.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class FixedScale : MonoBehaviour {
public float FixeScale =1 ;
public GameObject parent;
// Update is called once per frame
void Update () {
transform.localScale = new Vector3 (FixeScale/parent.transform.localScale.x,FixeScale/parent.transform.localScale.y,FixeScale/parent.transform.localScale.z);
}
}
Yesss I used this method from Rispat-$$anonymous$$omit and its work so well for my moving Platform; Thanks a lot. I tryed everything and never get a good result. Finaly you save me a lot of time and troubles.... :P.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEdit$$anonymous$$ode]
public class FixedScale : $$anonymous$$onoBehaviour {
public Vector3 scale;
public float FixeScaleX ;
public float FixeScaleY;
public float FixeScaleXZ;
void Start(){
scale = transform.localScale;
FixeScaleX = scale.x;
FixeScaleY = scale.y;
FixeScaleZ = scale.z;
}
// Update is called once per frame
void Update () {
transform.localScale = new Vector3 (FixeScaleX/parent.transform.localScale.x,FixeScaleY/parent.transform.localScale.y,FixeScaleZ/parent.transform.localScale.z);
}
}
Answer by rajat · Jul 25, 2013 at 10:40 AM
removing and re-adding children seems to work fine for me:
private void ChangeParentScale(Transform parent,Vector3 scale)
{
List<Transform> children= new List<Transform>();
foreach(Transform child in parent) {
child.parent = null;
children.Add(child);
}
parent.localScale = scale;
foreach(Transform child in children) child.parent = parent;
}
Answer by springwater · Oct 12, 2015 at 05:40 PM
here's my solution :P
Create an object/variable of the scale you want.. do not parent it.. its a template.. then during scaling operations check to see if your child object has exceeded one of the template's vector3 lengths.. if so.. scale the child object back to template size ^^
var child: GameObject; //child of some other object in another script perhaps.
var atemplate :GameObject;
Update(){
if(Child.lossyScale.x!=atemplate.lossyScale.x)
child.transform.localScale=atemplate.transform.localScale;
}
When I see it here, it appears that I have used the code snippet button.. the code is formatted and in a seperate box. Ive also tested the code in my game and it works.. heres my version..` that Ive tested and am using.. Its a better method i think because if the parent object is moving.. you never lose the parent child relationship for an instant..perhaps the parent movement is controlled by another script! Oh No! But I dont know.. maybe not:P
function sizeinvenpics(){
if (comswordpic.transform.lossyScale.x!=temp.lossyScale.x)
comswordpic.transform.localScale=temp.localScale;}
Answer by rockyourteeth · Oct 13, 2015 at 02:58 AM
You could make an empty parent object, and then make your two object siblings instead of parent/child. In other words, you have a parent with two children, one that you want to scale, and one that you don't. Now you can scale the one without affecting the other.
You can put your script on the parent, and the mesh, or whatever it is you're scaling, on the child.