• 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
0
Question by Paski-7 · Jan 18, 2020 at 04:40 PM · scaleparentscalingparent-childparent and child

Avoid scaling GameObject after parenting

Hello Community,

I have a GameObject (Plate) with the Scale (X=13 / Y=1 / Z=7)

and a GameObject (Item) with the Scale (X=1 / Y=0.05 / Z=1)


Now I have Script on the Plate (AttachItemScript)

     private void OnTriggerEnter(Collider other)
     {
         if (other.tag == "Item")
         {
             other.transform.parent = transform;
         }
     }
 
     private void OnTriggerExit(Collider other)
     {
         if (other.tag == "Item")
         {
             other.gameObject.transform.parent = null;
         }
     }

The Plate is moving and the item should moved too, (OnTriggerEnter).

It's possible, that an item can leave the plate (OnTriggerExit).


The Problem is, sometimes the Item exit the plate and the scale of the item is changing.

How can I avoid this?

Comment
Add comment · Show 35
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 mscardinal · Jan 18, 2020 at 06:24 PM 0
Share

@Paski-7 When looking at your code it looks like you try to set the transform of the parent of the colliding item to its parent´s transform(which includes rotation, scale and position) and then trying to nullify it when it exits the collider. It would be helpfull if you could post the purpose of the script. Then I can surely help you.

avatar image Paski-7 · Jan 18, 2020 at 06:37 PM 0
Share

@mscardinal thanks for your comment - The purpose of this code is: if an object (item) lands on the plate (the plate is moving), the item should move just like the plate. If somethink grab the item from the plate, the item should no longer be a child from the plate!

avatar image mscardinal · Jan 18, 2020 at 06:40 PM 0
Share

I see so do you have a trigger collider on the plate or a real?

avatar image Paski-7 · Jan 18, 2020 at 06:41 PM 0
Share

Yes, I have a Box Collider and the Trigger option is selected.

avatar image mscardinal · Jan 18, 2020 at 06:42 PM 0
Share

Does the item have gravity (rigidbody)?

avatar image Paski-7 · Jan 18, 2020 at 06:43 PM 0
Share

Yes, Rigidbody and Gravity is selected + $$anonymous$$esh Collider.

avatar image mscardinal · Jan 18, 2020 at 06:44 PM 0
Share

So the item falls trough the plate.(sorry going a bit offtopic)

avatar image mscardinal · Jan 18, 2020 at 06:45 PM 0
Share

Its a 3d game, isnt it?

avatar image Paski-7 · Jan 18, 2020 at 06:48 PM 0
Share

No, the items falls down and land on the plate, not through.

avatar image mscardinal · Jan 18, 2020 at 06:51 PM 0
Share

interesting. So back to the original question. Childs always scale with the parent so 2 possibilities would be to 1.set the scale of the child permanent to 1;0.05;1 by clamping it (i reccommend this) or 2.not scaling the parent but that would only work on a 2d game.

avatar image Paski-7 · Jan 18, 2020 at 06:51 PM 0
Share

Yes, 3D Game!

avatar image mscardinal · Jan 18, 2020 at 06:53 PM 0
Share

In case you dont know how to clamp id help you.

avatar image Paski-7 · Jan 18, 2020 at 07:01 PM 0
Share

Don't know how to clamping. Would be create, if you can help.

avatar image mscardinal · Jan 18, 2020 at 07:02 PM 1
Share

of course ill help

avatar image mscardinal · Jan 18, 2020 at 07:12 PM 1
Share

$$anonymous$$ake a script and put it on the item. Write: public Transform transform;

void Start() { transform = this.gameObject.transform; } void Update() { transform.localScale = new Vector3 ($$anonymous$$athf.Clamp(transform.localScale.x, 1f,1f),$$anonymous$$athf.Clamp(transform.localScale.y, 0.5f,0.5f ), $$anonymous$$athf.Clamp(transform.localScale.z, 1f,1f); }

could work but there can be some errors so just contact me :)

avatar image Paski-7 · Jan 18, 2020 at 07:13 PM 0
Share

Thank you very much, I'll give it a try later! :)

avatar image mscardinal · Jan 18, 2020 at 07:14 PM 0
Share

you know where to find me

avatar image Paski-7 · Jan 19, 2020 at 02:26 PM 0
Share

@mscardinal I've tried that. Now the effect has changed. private void OnTriggerEnter(Collider other) { if (other.tag == "Item") { other.transform.parent = transform; } } Now: OnTriggerEnter make the item (GameObject) very big.

      private void OnTriggerExit(Collider other)
      {
          if (other.tag == "Item")
          {
              other.gameObject.transform.parent = null;
          }
      }

And OnTriggerExit the item (GameObject) has the normal scale.

I'm going to try a few things now :D

avatar image mscardinal Paski-7 · Jan 19, 2020 at 02:46 PM 0
Share

So you tested my code?

avatar image Paski-7 · Jan 19, 2020 at 02:48 PM 0
Share

yes, but it didn't work

avatar image mscardinal Paski-7 · Jan 19, 2020 at 02:49 PM 0
Share

did you get errors or didnt it just do anything?

avatar image Paski-7 mscardinal · Jan 19, 2020 at 02:52 PM 0
Share

OnTriggerEnter: The Coin scale gets really big. OnTriggerExit: The Coin gets his old scale back.

avatar image Paski-7 mscardinal · Jan 19, 2020 at 02:53 PM 0
Share

Now the Problem is, the Item ENTER the plate and the scale of the item is changing.

avatar image mscardinal mscardinal · Jan 19, 2020 at 03:15 PM 0
Share

another not that performant but easy way would be to do this: (Script on Item)

     public Vector3 desiredScale;
     
     void Awake()
     {
      desiredScale = this.gameObject.transform.localScale;
     }
     void Update()
     {
       this.gameObject.transform.localScale = desiredScale;
     }
 
avatar image Paski-7 mscardinal · Jan 19, 2020 at 04:13 PM 0
Share

I have something that works, but need help with a little thing: I have a GameObject (Name: ItemParent) and under this GameObject, I have another one (Name: ItemChild).

     private void OnTriggerEnter(Collider other)
     {
         if (other.tag == "ItemChild")
         {
             // *Parent of ItemChild* should .transform.parent = transform;
         }
     }

How can I write this one line of Code? ( the line with the // )

avatar image mscardinal Paski-7 · Jan 19, 2020 at 04:15 PM 0
Share

what are you trying to do? you want the "itemparent" to be the parent?

avatar image mscardinal mscardinal · Jan 19, 2020 at 04:24 PM 0
Share

that would be

     this.gameObject.Transform = GameObject.Find("ItemParent").Transform;

avatar image Paski-7 mscardinal · Jan 19, 2020 at 04:34 PM 0
Share

Something like: itemChild.GetParent.transform.parent = transform;

avatar image Paski-7 mscardinal · Jan 19, 2020 at 04:32 PM 0
Share

We are only talking about 3 game objects. GameObject 1 = itemChild, GameObject 2 = itemParent, GameObject 3 = plateParent! In the Hierarchy itemChild is under itemParent. Now I need the code: itemChild have to say, bring me (itemChild) and my parent (itemParent) under the GameObject plate. So that the hierarchy looks like this:

 Plate
    itemParent
       itemChild

 

I'm sorry if I explain it a bit awkward.

avatar image mscardinal Paski-7 · Jan 19, 2020 at 04:48 PM 0
Share

If I understood right you want to call this from ItemChild? And you want to parent itemParent to Plate and itemChild is parented to itemParent. Is that correct?

avatar image Paski-7 mscardinal · Jan 19, 2020 at 04:49 PM 0
Share

100% correct! :D

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Esteem · Jan 18, 2020 at 10:25 PM

Instead of doing

 other.transform.parent = transform;


do

 other.transform.SetParent(transform, true);
Comment
Add comment · Show 2 · 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 mscardinal · Jan 19, 2020 at 10:58 AM 1
Share

@Esteem This wont work because the object would still copy the scale so it doesnt solve the problem.

avatar image Paski-7 · Jan 19, 2020 at 02:10 PM 1
Share

That did not work. I've already tried that!

avatar image
0

Answer by Magso · Jan 19, 2020 at 02:27 PM

Divide the item's scale by the plate's scale as it's set as the parent.

 item.transform.localScale = item.transform.localScale / plate.transform.localScale;
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 unity_C6916A6F7285458B303D · Mar 30 at 01:59 PM 0
Share

does this work?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

121 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Im trying to a child object follow the path of the parent in the path that the parent has already traveled... 1 Answer

How to preserve size of children when changing scale of parent 5 Answers

children doesn't take parent's rotation, position or scale. 1 Answer

Extraction of Coordinate and Scaling it down by half. 1 Answer

How to scale a parent without scaling children? 5 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