How to check if a player pressed a key?

I’m trying to implement player movements through a new vector3. Below is my script that I wrote.

    if (Input.GetKey(KeyCode.D))
    {
        MoveDirect.x = 2;
    }
    if (Input.GetKey(KeyCode.A))
    {
        MoveDirect.x = -2;
    }

The problem is that my character doesn’t want to stop. I tried to implement it through else but I couldn’t. I would be grateful for any help.

probably need a bit more code, but is movedirect set to 0,0 at the beginning or is a local variable thats new every time?

Yes, all movedirect axes are 0.
MoveDirect = new Vector3(0f, 0.0f, 0.0f);

maybe you just change velocity through .velocity = 1 (e.g and it just keeps at 1) or AddForce bla-bla ForceMode.Velocity and never get it back to zero when player stopped holding the button or the character physical material is zero friction?

No, if you change the value of movedirect, the character stops. I just don’t know how to implement a stop by lowering the keyboard key.

maybe you can just ask free ChatGPT 3.5 that was unlimited to everyone even on OpenAI website as I know, or show the whole thing with the important logic parts, it is hard to get when a few lines what exactly you want and happening. You can even give it the script if its simple and ask for bugfix, improvement

You set your velocity whenever you hold one of your keys down, but you never set it back to 0. So of course it would not stop moving. What you probably want to do is this:

    if (Input.GetKey(KeyCode.D))
    {
        MoveDirect.x = 2;
    }
    else if (Input.GetKey(KeyCode.A))
    {
        MoveDirect.x = -2;
    }
    else
    {
        MoveDirect.x = 0;
    }

In this case when you press A and D at the same time, D would dominate.

and why you want to stop when lowering the key? you mean it stops when you hold button instead of AFK? Then invert the logic just?

Thank you. That’s what I was trying to do.

What do you then do with your MoveDirect? can you share that bit of code?

You said you want it to stop by lowering a key? so you want to press a key to stop?

I definately your code would help. As from that small snippet it looks like you are adding it to a position so, if you only set it to 2 or -2 when pressed then when not pressed it should auto stop, because it is no longer changing the position… hence, wondering what the rest of the code is

@FnSolo you really are obsessed with AI arent you :stuck_out_tongue: AI is not bad, but understanding why your code doesnt do what you think is a valid thought process so I think we need to encourage @anadaly1 to understand why what they wrote hasnt done what they thought.

anyways moderator solved it i just didnt thought after he said about stop on lowering the key that it is that primitive as i thought Ed. and actually replied upper to set velocity back to zero

Ed. 2 Should’ve shown it in the else{} code not a tip

Another solution could be

    MoveDirect.x = 0;
    if (Input.GetKey(KeyCode.D))
    {
        MoveDirect.x += 2;
    }
    if (Input.GetKey(KeyCode.A))
    {
        MoveDirect.x -= 2;
    }

Here each if statement would add or subtract to / from the value. Here the behaviour would be when you press both buttons at the same time, you would get back to 0 as both if statements would be entered. One is adding 2 to 0 the other subtracting 2. So you’re back to 0.

Ah, so you mislead me and I fell for it, when i asked if you set it to zero you said yes, so you obviously arent doing it before this code to be a case of if no keys are pressed you arent moving… Hence the whole chunk of code would be usefu.

@Bunny83 did good by guessing you werent and what you did with your variables :smiley: