1
\$\begingroup\$

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?

\$\endgroup\$
4
  • 1
    \$\begingroup\$ I don't know if your approach is the best, but I understand what you are trying to resolve. The way I would buy about it would be to create a second script, empty game object and attach the collider and script to the empty game object. Make the empty object a child of the character and place where you need it to be, that way you'd have separate triggers. Makes sense? \$\endgroup\$ Commented Feb 3, 2022 at 22:03
  • \$\begingroup\$ Makes sense, but how do I make the collider on the child trigger an event in the parent script? \$\endgroup\$ Commented Feb 3, 2022 at 22:13
  • 1
    \$\begingroup\$ You don't. You create a public game object field on the child script and in the editor you assign the parent to it. At that point you have access to all components of the parent and you can manipulate it from the child. \$\endgroup\$ Commented Feb 3, 2022 at 22:50
  • \$\begingroup\$ Got it. Thanks, I'll give it a try! \$\endgroup\$ Commented Feb 4, 2022 at 3:28

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.