I’m currently working on an “examine” system, as well as suffering from the case of small brain. It was going well until I discovered that if I hold down, for example, “W” while entering examine mode on an object, my character keeps moving in that direction even though my movement system is disabled by a bool while examining.
I have tried setting the CharacterController.Move to a Vector3.zero, when the examine bool equals true, but my controller doesn’t really seem to care about that and keeps moving anyways. It’s like my GetAxis and CharacterController.Move values get stuck when the movement system gets disabled, and I have no idea how to make it… not do that?
If I stand still and enter examine mode it works just fine, making me unable to move until I stop examining.
I’m pretty new to coding (a few months of experience on/off over 2 years) and I usually solve my problems through research but I can’t seem to find anyone with a similar problem, so here I am.
I’m personally not a fan of == false or == true. I read ! as not, and the variable name should in my opinion also read as a boolean. (Like isGrounded does.)
Ontopic:
You toggle the examine value in OnExamine. Shouldn’t you just turn it to either true or false?
I’m calling OnExamine to set examine to true if it’s false, and false if it’s true via !examine. I call it when I start examining and when I stop examining.
I’m not sure that this is what he wants, because that would allow him to move in examine-mode, when at least one of them is false. That is, if he’s examining but not grounded, he could still move.
I guess the original version already accomplished what he wanted, which basically also excludes the case above. That is, he can only move when grounded and not examining.
The shorter version of his logic would be controller.isGrounded && !examine.
@OP
The issue is that you don’t reset the movement vector, that is, whenever your conditions are not met, the old movement vector will be used, because you stored it in an instance field.
With the current script, you can just remove the instance field completely and set up a new vector at the very beginning of Update.
However, if you need that movement vector to be an instance field, reset it at the very beginning of your Update function.
That makes total sense. I added moveDirection = new Vector3(0,0,0) at the top of my update function and it works flawlessly now. Thanks to you I can finally call my examine script complete, and move on with my project.
It’s basically what you were thinking from the very beginning, so you were already pretty close. Probably just had it in the wrong place or something. Good luck, happy coding.