Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Discussion Unity's Physics is just for show (and this is how I try to work around it) ...

Discussion in 'Physics' started by sasq64, Mar 5, 2024.

  1. sasq64

    sasq64

    Joined:
    Jan 28, 2018
    Posts:
    13
    Unity's physics system is fineif you don't care too much about what happens with the objects; falling boxes, debris, particles etc -- but....

    I am working on a project where I want more control over my objects. Where I need to to control which objects are allowed to move or collide, and often place them at specific locations, and know what those locations are.

    This is pretty hard to do in Unity. These are some of the steps I am taking to make this work. This can be seen as either a list of FYI work-arounds, or a cry for help on how to do this better:

    • All moveable objects in the world are set to isKinematic = true
    • When I pick up an object (becomes child of hand) or when an object becomes the child of another object (like a drawer) I _remove_ the RigidBody completely (and save a subset of the RigidBody poperties). This allows the object to move along with its parent.
    • When I release an object is the only time the object becomes non-kinematic; here I add the RigidBody again (if needed) and set isKinematic = false. Now the object is allowed to bounce of other objects before it comes to rest.
    • When a released object comes to rest, I set isKinematic = true again.
    • If the RigidBody has _interpolation_ on I need to turn it off and wait a frame (I think) before moving the object anywhere, because the interpolation sometimes (?) remembers the previous position of the object...
    Is there a better way... ?
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,085
    It seems the only time you need a "moveable" object to be a rigidbody is when the object is dropped to the ground. Then when it comes to rest you could just remove the rigidbody.

    If you want to move/teleport a rigidbody to a new location without interpolation occurring then it should be done from within FixedUpdate by setting transform.position or the rigidbody's position. Don't use MovePosition to teleport a kinematic rigidbody or it'll try to interpolate.
     
  3. sasq64

    sasq64

    Joined:
    Jan 28, 2018
    Posts:
    13
    Thanks, the FixedUpdate() thing helped me solve the interpolation problem.

    My new problem is that removing/adding rigid bodies means I can't rely on TriggerEnter / TriggerExit pairs to record the state of collisions... so I think I need to resolve "logic" collisions (is an object inside a container?) manually.

    Which begs the question: Why cant we check colliders manually with the Unity API? Why is there something like BoxCast() but not a normal BoxCollide() ?

    I will have to keep a list of container locations and iterate the list and do sphere/sphere or sphere/box collisions to keep it simple I guess...
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,657
    What does a "normal BoxCollide()" do? Tell you what the box collides with?
     
  5. sasq64

    sasq64

    Joined:
    Jan 28, 2018
    Posts:
    13
    What I would actually need is `Physics.OverlapBox` but the other way around. Like "give me all the BoxColliders in the scene that collides with this gameObjects (convex) colliders.

    What I will likely do now is something like:

    When a movable objects comes to rest, do a distSquared comparison against all containers, and for all that can potentially overlap I'll do a CheckBox() against the containers BoxCollider.

    EDIT: Currently I just use the containers OnTriggerEnter/Exit to add/remove objects from a "touching" list, but because of the reason above I can't rely on this when removing RigidBodies.
     
  6. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,085
    An object doesn't need to be a rigidbody to be a trigger. A trigger is a feature of the attached collider. A rigidbody is only needed on the object that enters the trigger. Although character controllers can also be detected by triggers, despite not having a rigidbody.

    You can make all your moveable items triggers or you could even leave them as regular gameobjects and do the collision detection on the player instead. So when your player touches any object it can test to see if that object is collectable or moveable.

    If your player is a character controller and your items aren't triggers then you'll need to use the OnControllerColliderHit event instead of OnTriggerEnter.
     
    Last edited: Mar 7, 2024
  7. sasq64

    sasq64

    Joined:
    Jan 28, 2018
    Posts:
    13
    Currently, my "player" is my VR hands, which do not have a rigid body. At least one of the object must have one or you get no callbacks.

    I will probably add physics to the hands so they collide with things, in which case I can check all collisions from the hand objects.