5
\$\begingroup\$

I'm having trouble with Unity's basic character controlling. After i implemented the crouching in the code below the Input.GetAxis("Horizontal") does not update (while the left control is pressed (crouching), Input.GetAxis("Horizontal") is always set to zero even if left or right arrows are pressed.

The reason why this happens is not because of keyboard buffer etc. because horizontal input is working while jumping, which is implemented pretty much the same way the crouching is implemented in the code below. Btw. no root motion is used. Here's the code:

void HandleInput()
{
    currentBaseState = animator.GetCurrentAnimatorStateInfo(0);

    var deadZone = 0.1f;

    verticalVelocity = moveVector.y; 
    moveVector = Vector3.zero;

    // print for testing       
    print(Input.GetAxis("Horizontal"));

    if (Input.GetAxis("Horizontal") > deadZone || Input.GetAxis("Horizontal") < -deadZone)
    {
        yRotation += Input.GetAxis("Horizontal");
        animator.SetFloat("Direction", Input.GetAxis("Horizontal"));
    }

    if (Input.GetAxis ("Vertical") > deadZone) 
    {
        speed += 0.1f;
        moveVector += new Vector3 (0, 0, Input.GetAxis ("Vertical") + speed);
    }
    else if(Input.GetAxis ("Vertical") < -deadZone)
    {
        if(speed >= 0)
            speed -= 0.1f;
        else
            speed += 0.1f;
        moveVector += new Vector3 (0, 0, Input.GetAxis ("Vertical") + speed);
    } 
    else 
    {
        moveVector += new Vector3(0, 0, Input.GetAxis ("Vertical") + previousSpeed);
        if(speed > 0)
            speed -= 0.1f;
    }

    animator.SetFloat ("Speed", Input.GetAxis ("Vertical"));

    totalRotation = yRotation * rotationMultiplier;

    transform.eulerAngles = new Vector3(0, totalRotation, 0);

    moveVector = transform.TransformDirection(moveVector);

    if (moveVector.magnitude > 1)
        moveVector = Vector3.Normalize(moveVector);

    Physics.Raycast (gameObject.transform.position, Vector3.down, out hitInfo, 0.5f); 
    if(hitInfo.distance < 0.5f)
    {
        if (enableJump)
        {
            if (Input.GetButtonDown("Jump"))
            {
                animator.SetBool("Jump", true);
                verticalVelocity = jumpSpeed;
                skillSlider.value -= 1f;
                if(skillSlider.value == 0f){
                    enableJump = false;
                    bunnyImage.gameObject.SetActive(false);
                    skillSlider.gameObject.SetActive(false);
                }
            }
        }
    }

    if (Input.GetButtonDown ("Crouch")) {
        animator.SetBool ("Crouch", true);
        crouching = true;
    }

    if (Input.GetButtonUp("Crouch")) {
        crouching = false;
    }

    if (currentBaseState.fullPathHash == crouchState) {
        if(!animator.IsInTransition(0)){
            if(!crouching)
                animator.SetBool("Crouch", false);
        }
    }

    if (currentBaseState.fullPathHash == jumpState) {
        if (!animator.IsInTransition(0)) {
            animator.SetBool("Jump", false);
        }
    }

    previousSpeed = speed;

    moveVector = new Vector3(moveVector.x, verticalVelocity, moveVector.z);

    if (moveVector.y > -terminalVelocity)
        moveVector = new Vector3(moveVector.x, moveVector.y - gravity * Time.deltaTime, moveVector.z);

    if (characterController.isGrounded && moveVector.y < -1)
        moveVector = new Vector3(moveVector.x, -1, moveVector.z);

    characterController.Move(moveVector * Time.deltaTime);

}
\$\endgroup\$
  • \$\begingroup\$ So to clarify what's going on, this line in your code is printing 0? print(Input.GetAxis("Horizontal")); \$\endgroup\$ – jhocking Nov 9 '15 at 18:08
  • \$\begingroup\$ while crouching, that is true yes. Even while i'm simultaneously pressing left or right arrows to try to turn the character. The idea is to add Input.GetAxis("Horizontal") to yRotation, but the horizontal input is always zero while crouching. \$\endgroup\$ – user3629146 Nov 9 '15 at 18:11
  • 1
    \$\begingroup\$ Off the top of my head, I would guess it's an issue with your keyboard; a lot of older keyboards have problems with multiple keys pressed at the same time. But that's just my first guess... \$\endgroup\$ – jhocking Nov 9 '15 at 18:15
  • \$\begingroup\$ not because of old keyboard etc because rotation works while jumping (not because of the amount of buttons pressed...) \$\endgroup\$ – user3629146 Nov 9 '15 at 18:17
  • \$\begingroup\$ I figured out that the problem is related to ctrl -button itself. When i changed that button to spacebar, the problem is fixed. The controls however should be implemented so that the crouch button is left control. \$\endgroup\$ – user3629146 Nov 10 '15 at 8:43
-1
\$\begingroup\$

The problem you are facing is called Keyboard Ghosting. Hardware limitations of each keyboard can lead to keys not registering the pulse when several keys are pressed. Laptops usually suffer from this problem more often.

Usually, keyboards tend to make the most typical keys to be pressable at the same time (for example WASD keys, to make sure FPS games work properly). This is a problem you will have to take into account when deciding the controls for your game, and unfortunatelly, there is no way to avoid this problem through software, its purelly on the hardware side.

There is an articles, with a visual explanation, here.

\$\endgroup\$
  • \$\begingroup\$ the user describes input not working when a single button is held down. That alone suggests it probably is not ghosting. The fact that this button is CTRL adds further emphasis that it would not be ghosting, as this combination is common in general notebook use, including word processing. \$\endgroup\$ – Gnemlock Sep 12 '16 at 15:56
  • 1
    \$\begingroup\$ @Gnemlock while I'd agree with you in theory, the author's comments above state that the problem disappeared when switching from CTRL to Space. That very strongly suggests something like ghosting of chords involving CTRL (even though that's a very unusual thing for a working keyboard to do!) Maybe the user had mapped a very unusual set of keys for movement, rather than the standard WASD (eg. right-ctrl + menu)? Or maybe they had another app running that intercepted keyboard events when CTRL was pressed? It's hard to know nearly a year later, as the asker doesn't seem to be a repeat visitor. \$\endgroup\$ – DMGregory Sep 12 '16 at 16:34
  • \$\begingroup\$ I had a similar problem in a laptop I used to work on, giving me this kind of behaviour while using the control key. It MAY not be this case, as @DMGregory pointed out, the user will probably never verify this, but at least this answer will help others having this problem. In these cases, what would be the approach when rating a question? \$\endgroup\$ – Leo Sep 12 '16 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.