Absolutely, the code you wrote should be fine! However, there must be some issues in the code you have or are running. I can’t help you diagnose the code as the code you show isn’t where the issue is stemming from.
If you create a new project and setup a isSwimming variable, it would probably react differently unless there is an underlying bug.