Search Unity

if (Input.GetKeyDown("Shift")) ERROR!!!

Discussion in '2D' started by CoderPing, Oct 23, 2021.

  1. CoderPing

    CoderPing

    Joined:
    Aug 28, 2021
    Posts:
    2
    Hi,

    im working at a 2D movement script but there is this error:
    ArgumentException: Input Key named: Shift is unknown
    UnityEngine.Input.GetKeyDown (System.String name) (at <963c3a1707f0469aac8b8356a8d18b5d>:0)
    MovementOfPlayer.Update () (at Assets/Scripts/MovementOfPlayer.cs:29)

    Here's my code:

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

    public class MovementOfPlayer : MonoBehaviour {

    public CharacterController2D controller;
    //public Animator animator;

    public float runSpeed = 40f;

    float horizontalMove = 0f;
    bool jump = false;
    bool crouch = false;

    // Update is called once per frame
    void Update() {

    horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;

    //animator.SetFloat("Speed", Mathf.Abs(horizontalMove));

    if (Input.GetButtonDown("Jump"))
    {
    jump = true;
    //animator.SetBool("IsJumping", true);
    }

    if (Input.GetKeyDown("Shift"))
    {
    crouch = true;
    } else if (Input.GetKeyUp("Shift"))
    {
    crouch = false;
    }
    }

    public void OnLanding ()
    {
    //animator.SetBool("IsJumping", false);
    }

    public void OnCrouching (bool isCrouching)
    {
    //animator.SetBool("IsCrouching", isCrouching);
    }

    void FixedUpdate ()
    {
    //Move our character
    controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
    jump = false;
    }
    }

    I comment the animator because I doesn't have any anmiations.
    Can anyone help me?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,474
    So you know, it doesn't matter that you're making something 2D, this post isn't about 2D features at all as it's you producing syntax errors in C# so should be posted in the scripting forum.

    Always posting code using code-tags because then, in the very least, line numbers make sense. Don't do arbitrary formatting such as bold/colour etc.

    Honestly though, don't use the string overload of that method, use the keycode overload because then at least you have a selection of keys and don't need to spell it correctly.

    Also, the link I just have you provides a link to the string key codes you can use but I'd stay away from them.
     
  3. orchard800

    orchard800

    Joined:
    Jul 28, 2015
    Posts:
    19
    You need to specify which shift key.

    Modifier keys: “right shift”, “left shift”, “right ctrl”, “left ctrl”, “right alt”, “left alt”, “right cmd”, “left cmd”