GetAxis keeps outputting when interrupted by boolean?

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.

Here’s my movement script:

public float speed = 6.0f;
public float gravity = 20.0f;
private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;
bool examine = false;

void Awake()
{
        // Movement
        controller = GetComponent<CharacterController>();
        gameObject.transform.position = new Vector3(0,0.8f,0);

void Update()
    {
        // Movement
        if(controller.isGrounded && examine == false)
        {
            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
            moveDirection = transform.TransformDirection(moveDirection);
            moveDirection = moveDirection * speed;
        }
     
        moveDirection.y = moveDirection.y - (gravity * Time.deltaTime);
        controller.Move(moveDirection * Time.deltaTime);
    }

And my examine script just calls this function when examining:

    public void OnExamine()
    {
        examine = !examine;
    }

I hope some of you smart people can help me out!

Always use parantheses to ensure that the Order of Evaluation of your expressions is what you intended:

if(controller.isGrounded && examine == false)

is evaluated as

 if(controller.isGrounded && (examine == false))

but you probably meant it to be

if((controller.isGrounded && examine) == false)

Thanks for the reply! That’s good to know, but it didn’t fix my current issue. :slight_smile:

And then personally I would write:

if((controller.isGrounded && examine) == false)

As:

if(!controller.isGrounded || !examine)

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?

examine = true;

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. :slight_smile:

1 Like

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.