I want my character to change the movement mode to swimming whenever he enters Water.
So I used the OnTriggerEnter to check if the other collider is my water volume, and if so, change movement mode to Swim, and that adds a ‘buoyancy’ force so the character floats. It ‘works’, but the issue is it triggers as soon as the character touches the water with his feet, and he floats like he’s walking on water. This is because my character collider is a capsule that is the size of the character.
So I added a second, smaller, box collider (set to trigger) to my character, attached to his head bone, and roughly the size of his head, so he would enter swim mode if his head touches the water.
The problem is, both colliders trigger the OnTriggerEnter event, and I don’t know how to tell them apart.
How do I know what collider is triggering the event?
Or is there a better way I can go about this? I’m coming from Unreal, and in Unreal each collider triggers its own event inside the same ‘actor’ script, but maybe that’s not the best way to do it in Unity?
You can add the trigger script to the smaller trigger box instead of the water, and check for intersection with water instead of the other trigger.
Alternatively, drop the second trigger, and have the water have a list of all the things it has triggered with. For all of those things, check if their center of gravity (or some other center that you define) is below the water surface, and in that case trigger the behaviour.