Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Collision detection happens way too early.

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

  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:
    667
    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:
    667
  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!