• 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 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 :)

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.

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

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

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

Destroying childs of a specific Parent 1 Answer

How to get an object to fill 3/4th of the screen. 0 Answers

smooth scailing (gui title image) 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