Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Object is changing size when becoming a child in game

Discussion in 'Scripting' started by Vaspix, Jul 17, 2019.

  1. Vaspix

    Vaspix

    Joined:
    Jun 8, 2019
    Posts:
    54
    Hey,
    I have a moving platform with a BoxCollider2D trigger, when the player is touching it, becomes a child so he can stand on the platform.
    Code (CSharp):
    1.  void OnTriggerEnter2D(Collider2D collision)
    2.     {
    3.         if (collision.gameObject == Player)
    4.             Player.transform.parent = transform;
    5.     }
    6.     void OnTriggerExit2D(Collider2D collision)
    7.     {
    8.         if (collision.gameObject == Player)
    9.             Player.transform.parent = null;
    10.     }
    But when I start moving him (rb.transform.Translate) he becomes smaller, my platform is 0.5 on x size, so when the player is attached, his x size is 0.5 too, but it's shown as 1 on x. How do I prevent this?
     
  2. GeorgeCH

    GeorgeCH

    Joined:
    Oct 5, 2016
    Posts:
    222
    Check the scale of the moving platform that the player is being parented to - is it anything other than (1,1,1)? Most likely, the player is just inheriting the scale of the parent object.

    To avoid this, create a parent object for the moving platform and set its scale to (1,1,1), then parent the platform and the player to that.

    So where you currently have this:
    • Moving platform
      • Player
    You would instead have this:
    • Parent object (scale 1,1,1)
      • Moving platform (scale whatever)
      • Player (scale whatever)
     
    Bunny83 and StarManta like this.
  3. Vaspix

    Vaspix

    Joined:
    Jun 8, 2019
    Posts:
    54
    Thank you!
     
  4. splitdisc4

    splitdisc4

    Joined:
    Jan 11, 2018
    Posts:
    1
    thank you from me as well GeorgeCH. you saved me,