• 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 · 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

120 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

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

How to move a gameObject with another gameObject but slower 1 Answer

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

smooth scailing (gui title image) 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges