Search Unity

Child doesn't follow parent

Discussion in 'Physics' started by jilleJr, Mar 29, 2015.

  1. jilleJr

    jilleJr

    Joined:
    Jan 21, 2015
    Posts:
    63
    I have tried setting my player object as a child to a moving platform so that it would follow it when moving, but my player doesn't move with the platform. Super strange...

    In my game I am trying to move the platform with the mouse, and I've tried 3 different methods: setting the raw position, changing the rigidbody velocity, and adding force.

    Method 1 - Setting the raw position
    Code (CSharp):
    1. rbody.position = Vector2.Lerp(rbody.position, mouse, Time.deltaTime * speed);
    Method 2 - Changing the rigidbody velocity
    Code (CSharp):
    1. rbody.velocity = Vector2.ClampMagnitude(deltaDist * speed, maxSpeed * 10);
    Method 3 - Adding force
    Code (CSharp):
    1. rbody.AddForceAtPosition(deltaDist * speed, transform.position);
    But none worked. In the hierarchy I have it setup like this
    • Platform
      • Player
      • Platform collider
    The platform parent contains the script for moving the platform with the mouse, and a rigidbody 2D. The platform collider contains the sprite renderer and collider 2D for the parent. I have separated the platform and it's collider to be able to scale the sprite and it's collider without scaling the player when it's set as a child to the platform.

    I have been googling around for solutions to make the player follow a platform and making it a child of the platform on collision seems as the best option, but for some reason as said earlier it doesn't follow the platform parent.

    I am double checking and the platform parents transform is changing and not the collider child, but the player objects transform is changing in reverse, to counter the transform changes instead of following them.
    I've tried disabling all extra scripts and everything but I can't get it to work. Any ideas why this is happening?
     
  2. waythewanderer

    waythewanderer

    Joined:
    Apr 6, 2015
    Posts:
    92
    Is your player object a rigidbody? Child objects are meant to inherit the collider properties of the parent transform, if I'm right. Having separate collider properties between the parent and child objects seems to be a very bad idea..
     
    pixiepangolin likes this.
  3. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    not enough code/information.

    is the above code on the platform or the player? Are you running anymovement code on the player
     
  4. jilleJr

    jilleJr

    Joined:
    Jan 21, 2015
    Posts:
    63
    I think I got it figured out why it acted as is did. Having a rigidbody on the player and the platform parent confused the rigidbody component on the player so it didn't follow it's changes. If I changed the transform position directly the player object would follow but changing the platforms rigidbody position only moved the platform and not the player, even tho the player was set as a child.

    I am running the code I viewed in my examples. I think I specified enough information. And the code is on the platform of course, otherwise it would move the player not the platform? o_O