Search Unity

Creating an impassible object

Discussion in 'Physics' started by FrenchToast112, Jul 22, 2015.

  1. FrenchToast112

    FrenchToast112

    Joined:
    Jun 30, 2013
    Posts:
    16
    I am trying to create an impassible object (i.e. invisible wall). I have been using a combination of components to the object but I have not found a solution.

    I have added ridbodies, and colliders to the wall but the ship object manages to always pass through or simply push it

    My ship object is structured like this

    player_ship
    + Box collider - IsTrigger not enabled
    + Ridgidbody - IsKinematic enabled
    <Child Obj of player_ship- ShipBoundary>
    + Box Collider - isTrigger enabled
    + Rigidbody - nothing enabled​
    <Child Obj of player_ship - ship>
    + Sphere collider - IsTrigger enabled​

    I need to have these things enabled for the various scripts attached to each of these objects.

    What would I need to do to create an impassible object for this object?
    Thanks.
     
  2. Aldo

    Aldo

    Joined:
    Aug 10, 2012
    Posts:
    173
    how are you moving this object, translate or force?
     
  3. FrenchToast112

    FrenchToast112

    Joined:
    Jun 30, 2013
    Posts:
    16
    I am moving the ship object with translate
     
  4. Aldo

    Aldo

    Joined:
    Aug 10, 2012
    Posts:
    173
    translate can trespass walls, specially when the object is kinematic
     
  5. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    youll have to use the correct physics functions if you want it to behave correctly.

    any of these... MovePosition/MoveRotation/AddForce/AddTorque
     
  6. Xaon

    Xaon

    Joined:
    Feb 28, 2013
    Posts:
    62
    @pepperbot I would suggest to turn off isKinematic on ship. Rigidbody with isKinematic won't be pushed by collisions.
     
  7. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    wont make any difference if the correct move functions arnt used.
     
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    To expand on the issue: translating an object simply sets it's new position. The physics system is not taken into account in any way.

    To move using physics, you either add forces to it with the Rigidbody's AddForce method, or you set the Rigidbody's velocity directly. The second one is a lot easier to deal with if you're a beginner, as you can replace your transform.Translate(direction) with rigidbody.velocity = direction.

    Also, kinematic rigidbodies will not move by velocity, so make sure to turn that off.
     
  9. Aldo

    Aldo

    Joined:
    Aug 10, 2012
    Posts:
    173
    Actually "MovePosition" works great, I tested it out after JamesLeeNZ pointed it out. And it does the job of transform.position
     
  10. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Not quite.

    Rigidbody.MovePosition moves the rigidbody to a certain position. If that position is inside a wall, the rigidbody will be pushed out of the wall again through physics.

    If that position is on the other side of that wall, though, the rigidbody will teleport through. Unless you have interpolation settings on, the MovePosition method isn't in any way different from just setting the transform's position.
     
  11. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Ive never seen a rb push through a collider with moveposition.

    Maybe I wasnt using enough force *ba-dum-pishhh*
     
    Aldo likes this.
  12. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,532
    MovePosition should actually be respecting the wall in 5.x. Might have to use Continuous Dynamic mode, though.
     
  13. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Tried it, it went straight trough. Didn't even get collision messages.
     
  14. bart_the_13th

    bart_the_13th

    Joined:
    Jan 16, 2012
    Posts:
    498
    If you insists on using translate, check with Raycast/SphereCast/CapsuleCast first before translating...