Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
7
Question by Dylan Cristy · Jan 26, 2011 at 05:58 AM · scaleparentchildrenindependent

How to scale a parent without scaling children?

I am trying to scale up an object without scaling it's children. The original scale vectors for both parent and children are (1, 1, 1). The parent is named and tagged differently than the children, but all children are named and tagged the same.

This code works to scale everything (including children) up:

var largeSize : float; // exposed in inspector

function ScaleUp () { var velocity = Vector3.zero; var targetScale = Vector3(largeSize, largeSize, largeSize);

 while (transform.localScale != targetScale)
 { 
     transform.localScale = Vector3.SmoothDamp(transform.localScale, targetScale, velocity, 0.5);
     yield;
 }

}

But I don't want everything scaled up, so I tried to differentiate the children and offset the scaling by scaling them back down. Since for (var child : Transform in transform) does include the parent transform, I tried to differentiate the children by name or by tag, as follows:

function ScaleUp () { var velocity = Vector3.zero; var targetScale = Vector3(largeSize, largeSize, largeSize); var childScale = Vector3(largeSize/2, largeSize/2, largeSize/2);

 while (transform.localScale != targetScale)
 { 
     transform.localScale = Vector3.SmoothDamp(transform.localScale, targetScale, velocity, 0.5);
     for (var child : Transform in transform)
     {
         if (child.gameObject.tag == "ChildSpecificTag")
             child.localScale = Vector3.SmoothDamp(child.localScale, childScale, velocity, 0.5);
     }
     yield;
 }

}

This... apparently works, but also catches the parent transform in an if statement it shouldn't be caught in? The result of this code is that no scaling occurs, everything stays the same as it was. But the parent is definitely tagged differently than the children. And also named differently, and the same thing happens (no scaling - or, everything is scaled up and immediately scaled back down) if the if statement is: if (child.gameObject.name != this.gameObject.name)

How can I apply scaling to the parent without scaling the children, or, how can I effectively differentiate the children to offset the scaling?

ETA: I have not tried GetComponent(s)InChildren because that apparently also returns the same component in the parent, and if the parent can evade an if statement designed to weed it out when returned through Transform in transform, will it be any different using GetComponent(s)InChildren?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
21
Best Answer

Answer by Jean-Fabre · Jan 26, 2011 at 06:58 AM

Hi,

I am sure there is a way to get your script working, but I would discourage you from doing it. I would strongly suggest adding a layer of indirection in your hierarchy instead.

Instead of linking your models together and being stuck when willing to scale one independantly, create an empty gameObject name "parent" like so:

 -- "parent"   
    -- "model to scale"   
    -- "another model"
       -- "yet another model"

Everything that you did normally with "model to scale" should now be done via "parent", and for scaling, you simply scale "model to scale" without interfering with the rest of the hierarchy.

I always do that when I need that kind of feature implemented, it's very common for technical assemblies and complex rigs. In assemblies, I even go further and have a layer of indirection of each and every feature rich model, so that I can exchange models, load them on demand, or complex Level of details scripting, basically mess with them without affecting in anyway the actual hierarchy itself.

Hope this helps,

Jean

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dylan Cristy · Jan 26, 2011 at 08:58 PM 1
Share

Ok, so it took me a while to adjust all my scripts so that everything is functioning properly, but it's all working. The one interesting thing I noticed is this - my original "model to scale", when it was the parent object itself, had a collider and rigidbody attached. Of course I had to move the rigidbody to the new "parent" so that it would move correctly, and I left the collider on the "model to scale" so that it would scale along with the model. I removed the script from the "model to scale" and put it on the "parent." [continued below]

avatar image Dylan Cristy · Jan 26, 2011 at 09:03 PM 0
Share

However, the script on the "parent" is successfully receiving OnCollision information from the collider that's on its child object. How is that working? Does collision information automatically travel up the hierarchy? Or up /and/ down the hierarchy?

I was expecting to have to attach a script to the "model to scale" object to manually send the collision info to the script on the parent. So, it's working out much easier than anticipated, but I want to really understand the behavior that's going on.

avatar image Jean-Fabre · Jan 27, 2011 at 04:49 AM 0
Share

Hi Dylan, make it another question please, then we keep things tidy :) thanks.

avatar image
5

Answer by Berenger · Jan 26, 2011 at 08:43 AM

Jean's answer is probably the best way to do it, but I have something else you can try; You can detach children, scale and re-attach them after. It might be costly with a lot of children though. Something like that :

public class ScaleAndParenthood : MonoBehaviour { public float s = 1.0f; private Transform[] children = null;

 // We build an array with all children, so we can re attach them later
 void Start () 
 {
     children = new Transform[ transform.childCount ];

     int i = 0;  
     foreach( Transform T in transform )
         children[i++] = T;
 }

 // Update is called once per frame
 void Update () 
 {
     if( s != transform.localScale.x )
     {
         transform.DetachChildren();                     // Detach
         transform.localScale = new Vector3( s, s, s );  // Scale        
         foreach( Transform T in children )              // Re-Attach
             T.parent = transform;
     }
 }

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dylan Cristy · Jan 26, 2011 at 09:04 PM 0
Share

Thanks Berenger, I tried this solution too, and it does work. And in my case it is fine since there would only ever be two children to detach / re-attach.

avatar image
4

Answer by Rachan · Aug 29, 2017 at 06:15 PM

You can do like this. it's work perfectly for me.

   Vector3 scaleTmp = object.transform.localScale;
     scaleTmp.x /= parent.localScale.x;
     scaleTmp.y /= parent.localScale.y;
     scaleTmp.z /= parent.localScale.z;
     object.transform.parent = parent;
     object.transform.localScale = scaleTmp;


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image stratos_kakalis · Sep 08, 2017 at 01:14 PM 0
Share

Thank you Rachan! In my case your answer worked great. I had thought of the same technique but was a little bored to dive in math so i figured why not google it and although the "correct" answer seemed to work for a lot of people but it didnt for me (maybe unity fixxed that "hackish" way to do it). However your little piece of code was perfect!

avatar image
0

Answer by V-Jankaitis · Jan 12, 2017 at 04:38 PM

if (Size != Old_Size && SphereColider != null) { SphereColider.radius *= Old_Size / Size; } transform.localScale = new Vector3(Size, Size, Size); Old_Size = Size;

important part is "*= Old_Size / Size;" multiply that by child scale and it will keep consistent size

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Rispat-Momit · Sep 07, 2018 at 08:21 PM

Hi there! I figure out this solution :D

Just set this script to your child ;) (It works also 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);
 
             }
         }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Script to add script to all children 1 Answer

Update Parent/ Children From Script? 2 Answers

How To Get List of Child Game Objects 14 Answers

How to check parent value instead name value(more details in post) 3 Answers

Is there a way to limit a parent object's child count to only 1 without individually doing it with each transform? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges