Search Unity

How to move objects that have gravity without breaking Colliders

Discussion in 'Scripting' started by Zellybeanz, Jun 15, 2019.

  1. Zellybeanz

    Zellybeanz

    Joined:
    Jun 10, 2019
    Posts:
    7


    This is what happens to my pieces if I follow the documentation, so what is the correct way to move them?
    For example, if (Input.GetKey(KeyCode.UpArrow)) in FixedUpdate, I just want the position of a rigidbody that's at (0,0,0) to now be at (0,0,1). It would be nice if there's a smooth transition but not necessary. I've tried transform.position and rigidbody.MovePosition so far. The first one works ok when there's only one object in play, but if it collides with another piece, then the overlap like above happens, or pieces bounce around a bit, or it just decides to move by a little more or less than 1 unit. Following documentation for MovePosition is eve worse: https://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html


    https://imgur.com/a/fpzxZqZ
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
  3. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,442
    Seems like the "set rigidbody.position" most resembles your situation, but you're going to have to mark it kinematic and compute collisions yourself.
     
  4. Zellybeanz

    Zellybeanz

    Joined:
    Jun 10, 2019
    Posts:
    7
    How would I do that? When i set it kinematic I can make it fall down by changing its y every frame, but then all I can do to make it stop falling is have it check whether the parent cube is still above the y of the floor. I need it to detect whether any of the 4 box colliders (or one mesh collider if I rebuild everything in Blender) are touching any collider that belongs to a different object and ignore if it's touching itself. It absolutely cannot overlap with any of these pieces. If I hit the right arrow to move it and that would cause an overlap, it should not be able to move in that direction.