I haven't found any information related to this. So the best solutions I can think of are only workarounds and there probably isn't any built in solutions to do this. It is very inconvenient indeed.
[SerializeField] private GameObject _hammerHeadGameObject;
[SerializeField] private LayerMask _impulseLayerMask;
[SerializeField] private CinemachineImpulseSource _cinemachineImpulseSource;
[SerializeField] private GamepadHapticSettings _hapticSettings;
private void OnCollisionEnter(Collision other)
{
if ((this._impulseLayerMask & (1 << other.collider.gameObject.layer)) != 0)
{
for (int a = 0; a < other.contactCount; a++)
{
if (other.contacts[a].thisCollider.gameObject == this._hammerHeadGameObject)
{
this._cinemachineImpulseSource.GenerateImpulse();
InputSystem.GetDevice<Gamepad>().TriggerHaptic(this._hapticSettings);
}
}
}
}
private void OnTriggerEnter(Collider other)
{
}
This is an example of how I get colliders from Collision
. Not the most efficient solution, but it works. The problem with Collider
- there is a high chance that it's not possible to get contacts from it, at least I haven't found how to.
1.
We can simulate our own collision to get information. The process is pretty straightforward:
- Trigger collision.
- Work out what type of collider it is.
- Simulate collision with same settings (bounds, size, offset...).
- Get colliders that were hit (those will be the ones you are looking for).
Small example:
private void OnTriggerEnter(Collider other)
{
if ((other as BoxCollider) != null)
{
BoxCollider boxCollider = (other as BoxCollider);
Collider[] colliders = Physics.OverlapBox(boxCollider.center, boxCollider.size / 2f, boxCollider.transform.rotation); // I am not sure about (/ 2f) - not tested.
}
}
Basically we solve this from another perspective from original. We take the collision information from other colliders that hit the colliders we need information about.
Same approach is taken in second solution.
2.
In this approach we process collision in reverse. Instead of finding the collision in the original object. We have the script attached to other objects that can collide with it. And upon collision we notify that object that it has been hit and pass it a collider that was hit.
private void OnTriggerEnter(Collider other)
{
if (other.TryGetComponent<ObjectThatCanBeHit>(out ObjectThatCanBeHit objectThatCanBeHit))
{
objectThatCanBeHit.CollisionHappened(other); // There we process that collision.
}
}
All in all, the first solution is more flexible, but requires more processing power if there are lots of collisions. Although, the second one can be intense as well, it all depends on quantity of objects in the scene and number of collisions. So some kind of benchmark would be required, I am just saying that first one would require more processing power from the first look and intuition.