Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to make a rigidbody sit on another rigidbody while moving

Discussion in 'Scripting' started by BmxGrilled, Feb 26, 2015.

  1. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    238
    Hi, I'm having difficulty solving a problem and could really do with some help.
    I have a situation in a game im working on, where I have a pirate ship, which has a mesh collider and a rigidbody. Which is moving forward across a water simulation and rocking above etc. And I have the player which is using a rigidbody too. When the player is on the decks of the ship, and the ship is moving, the player is sliding about and not sticking on the boat, this is causing problems with my ladder script for instance, where the player can climb but as soon as the ladder releases the player, the player flies across the ship to the rear castle deck. Comical to look at but annoying to figure out ways of solving this. I'm at a loss, any advice, tips, suggestions, will be much appreciated. I can't change the player or ship from using a rigidbody though, so answers oriented around this fact will be greatly appreciated, thanks in advance.
     
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    The easiest answer would be to use a fixed joint: http://docs.unity3d.com/Manual/class-FixedJoint.html

    That said, I had a similar problem with a project of mine (using space ships instead of pirate ships)
    Where I wanted to have dozens of moving objects inside a moving ship, and using a joint for each of them seemed wasteful.

    Instead what I went with was using an "interior" representation of the ship, which is simply a collider attached to a non-moving game object on a separate collision layer.

    When crew members are inside the ship, their rigid-bodies would be disabled and they would have interior physics representations on the ships interior representation, and their crew would simply obtain position updates by getting the relevant data them from their representations. When they are outside the ship, their rigid-bodies would be enabled again.

    This way you can have the ship bouncing and floating in the waves, and all of the crewmen on the ship wouldn't slip and slide off of it.
     
  3. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    you may want to set the Ship as parent of the player transform...
    what happen if you do that?
     
  4. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    If you're using physics, that doesn't work.
     
  5. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    oh yes.. you're right.. Fixed Joint is the way..
     
  6. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    238
    Im sorry however I dont see how a fixedJoint could work, I've tried it, it gets the character to stick to the deck of the ship, and I have tried setting the connectedBody to null and reconnecting after using MovePosition, and I cant get the character to move at all :3
     
  7. Theangrybagel

    Theangrybagel

    Joined:
    Dec 16, 2017
    Posts:
    2
    I've got the same problem m8.
     
  8. Theangrybagel

    Theangrybagel

    Joined:
    Dec 16, 2017
    Posts:
    2
    Edit: I just figured it out... I think. Parenting the rigidbody seemed to work for a spaceship I was making that let the player move while the spaceship was flying. I guess you can try parenting @TwixEmma
     
  9. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Another solution is to keep the ship fixed in place and move the universe around it to give the impression the ship is moving, I think the game "Sea of thieves" does it like and and it could also be applied to a space ship.