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

Best way to rotate a parent with a child rigidbody

Discussion in 'Physics' started by rebelincontrol, Apr 19, 2020.

  1. rebelincontrol

    rebelincontrol

    Joined:
    May 19, 2015
    Posts:
    30
    Hey -

    I'm making a puzzle game where you rotate a 2D cage around a central pivot point, in order to guide a physical ball to a target. The best way to do this until now has been rotating the transform of the cage (the parent) and having the rigidbody as a child of this. This allows the ball to move along with the cage which is the intended behaviour.

    An issue arose when I started adding levels with gaps in the walls (so the ball could fall out). The rotation of the cage would continue affecting the ball which it of course shouldn't. I tried unparenting the ball when it leaves the bounds of the cage, but the change in feel is so immediate it doesn't work. This approach doesn't lend itself to scenarios where the ball actually has to go around the outside of the cage to get to its target. The feel of the physics completely changes so it's a bad experience.

    I decided to make the cage rotate itself using physics (MoveRotation), and unparent the ball permanently, so the entire mechanic used physics. This works when the ball leaves the cage, (the ball is completely independent of the cage), but when it is inside, the ball does not take any influence from the cage rotation.

    Is there a way to make the cage rigidbody rotation affect the ball? So it rotates with it?

    Or perhaps there is a better approach to this that i'm missing entirely? Any help would be hugely appreciated I've been trying to get this right for a few days now.

    Attached a screenshot of a level so you can see what the cage/ball thing is all about. The black blob with eyes is the ball.

    Cage is probably the wrong word.. initial levels the ball was trapped inside it so that's what I've gone with!