Search code examples
c#unity-game-engine3dkeypress

Different movements upon Shift+W? It keeps just entering the "W" branch and not letting me exclusively code Shift+W


OK, so I'm trying to make different player animation & speed happen when player hits Shift+W, as opposed to just W.

Here's the working code for just W:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MherControls : MonoBehaviour
{

    float speed = 2;
    float rotSpeed = 80;
    float rot = 0f; //0 when we start the game
    float gravity = 8;

    Vector3 moveDir = Vector3.zero;

    CharacterController controller;
    Animator anim;

    // Start is called before the first frame update
    void Start()
    {
        controller = GetComponent<CharacterController>();
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        //anim condition 0 = idle, 1 = walk, 2 = run

        if (controller.isGrounded)
        {
            if (Input.GetKey(KeyCode.W))
            {
                anim.SetInteger("condition", 1); //changes condition in Animator Controller to 1

                moveDir = new Vector3(0, 0, 1); //only move on the zed axis
                moveDir *= speed;                 

                moveDir = transform.TransformDirection(moveDir); 


                if (speed < 10){
                    speed += Time.deltaTime; //max speed is 10
                    //Debug.Log(speed);
                }
                if (speed >= 2.5)
                {
                    anim.SetInteger("condition", 2);
                }



            }
            if (Input.GetKeyUp(KeyCode.W))
            {
                anim.SetInteger("condition", 0); 
                speed = 2;
                moveDir = new Vector3(0, 0, 0); 
            }



            rot += Input.GetAxis("Horizontal") * rotSpeed * Time.deltaTime; //horizontal are A and D keys and also left and right arrows
            transform.eulerAngles = new Vector3(0, rot, 0); //our character's transform property

        }
            //every frame move player on y axis by 8. so lowering to the ground
            moveDir.y -= gravity * Time.deltaTime;
            controller.Move(moveDir * Time.deltaTime);
    }
}

However, when I try to introduce Shift+W behavior, example:

if ( (Input.GetKey(KeyCode.W)) && (Input.GetKeyDown(KeyCode.LeftShift)) {
      speed = 2;
      anim.SetInteger("condition", 1);
}

Then it doesn't work. It just keeps going into the W branch, and never lets me code behavior exclusively for Shift+W.

What am I doing wrong? How can I make different behavior for when player holds Shift+W, which is different from when player is holding only W?


Solution

  • You need to switch the way you are checking for keys. GetKeyDown is only true for the frame you press the key (https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html), and GetKey remains true while the key is held down (https://docs.unity3d.com/ScriptReference/Input.GetKey.html). So to hold shift and then press W the check should be

    if ( (Input.GetKey(KeyCode.LeftShift)) && (Input.GetKeyDown(KeyCode.W)) {
          speed = 2;
          anim.SetInteger("condition", 1);
    }