Search Unity

Manual Trigger Collision Detection via Script

Discussion in 'Physics' started by Deosis, Feb 14, 2017.

  1. Deosis

    Deosis

    Joined:
    Feb 28, 2013
    Posts:
    2
    Hi,
    I am working with a generative grammar that replaces placeholder prefabs with new objects, some of witch have again placeholder objects etc. First I was working on a grid, but now I want to loosen the script from a fixed grid. My problem now is how to avoid generating overlapping structures, witch I don't want to have. I thought about after instantiating a new prefab, I could just test if it collides with any existing prefab it shouldn't collide with, and if it does, I delete it and try another one. But as it seems there is no way to manually ask for a collision detection in a C# script.
    I found workarounds with Physics.SphereCast or Physics.BoxCast and such, but I would appreciate if I don't have to approximate all my models.
    Is there any way one could achieve a manual collision detection in C#?
    Thanks you for your help.
     
  2. Deosis

    Deosis

    Joined:
    Feb 28, 2013
    Posts:
    2
    As 00jknight pointed out on reddit: Physics.ComputePenetration does exactly that. I still have to iterate over all my GameObjects to get their colliders, but I'll write a custom function for that.
    NOTE: Physics.ComputePenetration is only available in Unity 5.6
     
    orenwang likes this.