Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Collision detection happens way too early.

Discussion in 'Physics' started by narutkataaa, May 13, 2018.

Thread Status:
Not open for further replies.
  1. narutkataaa

    narutkataaa

    Joined:
    Apr 29, 2015
    Posts:
    9
    Hello!

    The concrete case:
    I have multiple game objects with convex mesh colliders and rigid bodies that are not moving.
    Those are being instantiated over and over again next to each other.

    The goal:
    I am using the convex mesh colliders and the rigid bodies to check if there is space for the game object after instantiation.

    The idea is that when a collision occurs - then there is no space and the game object should be destroyed. If not - then the game object remains.

    The problem:
    OnCollisionEnter is called even when the colliders are not touching, but are somewhat close to each other.

    Check this image.

    The execution:
    I attach a script on the game objects with the colliders that is to detect collisions.
    Code (csharp):
    1. public class RoomCollider : MonoBehaviour {
    2.     public bool IsSpace { get; private set; } = true;
    3.  
    4.     void Start()
    5.     {
    6.         IsSpace = true;  
    7.     }
    8.  
    9.     void OnCollisionEnter(Collision collision)
    10.     {
    11.         IsSpace = false;
    12.     }
    13. }
    14.  
    An object is instantiated, the convex mesh colliders and the rigid body are added.
    Code (csharp):
    1. // A collider is instantiated based on a game object with multiple mesh colliders.
    2. GameObject generationCollider = Instantiate(collider, room.transform);
    3.  
    4. // It is then scaled down a little.
    5. float scaleDown = 0.95f;
    6. generationCollider.transform.localScale *= scaleDown;
    7.  
    8. // A rigid body is then added.
    9. Rigidbody rigidbody = room.AddComponent<Rigidbody>();
    10. rigidbody.useGravity = false;
    11. rigidbody.constraints = RigidbodyConstraints.FreezeAll;
    12. rigidbody.useGravity = false;
    13.  
    14. // The mesh colliders are marked convex.
    15. MeshCollider[] generationMeshColliders = generationCollider.GetComponentsInChildren<MeshCollider>();
    16. foreach (MeshCollider meshCollider in generationMeshColliders)
    17.     meshCollider.convex = true;
    18.  
    The script waits for the FixedUpdate execution and then checks if a collision has occurred trough the "IsSpace" property.

    What I have tried (and found on the forums):
    Potential cause: Usually small scaled objects produce such behaviour
    My case: My objects are scaled upwards and not downwards, but I played with scales and this shouldn't be the case.

    Potential cause: Unity predicts collision based on movement.
    My case: My objects don't move.
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    Sorry if this is a dumb question, but I don't see how the collision event can be firing without the objects overlapping. So, are either of these possible?
    • The object is starting out at one place, and you move it to a new place after instantiation, but it still triggers collision based on its initial position? (Confirm this by logging the objects' position in OnCollisionEnter to make sure it's occurring when the object is where you think it should be.)
    • Are you sure that the OnCollisionEnter is firing because of these two colliders, and that it isn't another collider triggering the collision with one of the objects?
    Again, sorry if this isn't helpful, but that does seem like bizarre behavior if collision is occurring before the objects actually overlap...
     
  3. narutkataaa

    narutkataaa

    Joined:
    Apr 29, 2015
    Posts:
    9
    I just verified that the collisions are happening between the correct colliders and at the correct positions.

    Offtopic: Gravia seems pretty cool.
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
  5. narutkataaa

    narutkataaa

    Joined:
    Apr 29, 2015
    Posts:
    9
    Doesn't seem to be the case, but I played around and found the following:

    Using box colliders instead of convex mesh colliders seems to not reproduce the problem, so this is the workaround that I am using now.

    Thank you for your answers!
     
  6. BeGunity

    BeGunity

    Joined:
    Mar 9, 2017
    Posts:
    3
    Incase someone has similar issues I think what solved it for me, after you mentioned the issue didnt occur with box colliders, was switching the cooking options that go along with the mesh collider to none. If it is still colliding without touching it's more descrete than with the cooking options on.
     
  7. iceikking888

    iceikking888

    Joined:
    Oct 17, 2022
    Posts:
    1
    Just found out, there is the collision offset variable. By default (or at least in my Unity Project) it was set to 0.1. Set it to close to zero and it fixed my issues.

    To change the collision offset, go to:
    Edit > Project Settings > Physics > Default Contact Offset

    Again, set it close to zero.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,326
    It's non-zero for a reason. You should try to understand what it's for before changing this but in the end, it's up to you.

    Also, please don't necropost.
     
Thread Status:
Not open for further replies.