Search Unity

How to fix rigid body in position then move with player controller

Discussion in 'Scripting' started by ceteri, Mar 25, 2015.

  1. ceteri

    ceteri

    Joined:
    Mar 12, 2015
    Posts:
    11
    I am trying to implement a 3d game of Jenga, on a mouse click i do a raycast to see what object is on a reticle in the centre of the field of view. I would then like to lock the selected Jenga block to the player controller so that when the player moves the block moves with it. what is the best way to do this? i intially thought about making the controller the parent of the block, but then read about using a FixedJoint.

    I havn't tried doing either of these in code before so any examples would really help. I'm not sure what the best approach would be.
     
  2. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    244
    FixedJoints are only used if you want to make a join that can break.

    Just parent the object using their transforms:
    Code (CSharp):
    1. jengablock.transform.parent = player.transform;
    Also be sure to set 'isKinematic' to on for the jengablock, otherwise it'll fall/float due to the gravity physics.
     
    ceteri likes this.
  3. ceteri

    ceteri

    Joined:
    Mar 12, 2015
    Posts:
    11
    Thankyou, this helped a lot
     
  4. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
    The other way you might want to do this is apply force to the block using the character controller. As they pull/push, just apply force in the appropriate direction. Unless you are set on grabbable blocks, then it won't help :).