简体   繁体   中英

Unity C# GetKey() not working when i set a variable with the button i want pressed in

Im trying to make it so that you can assign whatever key you want to make the player jump/move via unity itself and not the script. I want to set public variables with the keycodes of each key and use these keys to start the functions. It doesnt work for the arrow keys and i cant find much info on the GetKey() keycodes. I am aware of the thing you can do on the project settings that sets a key for each action and then call the action but i dont want to do that.

Here is my code: // the script name is "Movement" and on the object is attached a rigidbody2D with the gravity on 1.5 and a box collider2D

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

public class Movement : MonoBehaviour
{
    public int JumpHeight;
    public int MoveSpeed;
    private Rigidbody2D rb;
    public string JumpButton;
    public string MoveRightButton;
    public string MoveLeftButton;
    // Start is called before the first frame update
    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    private void Update()
    {
        if(Input.GetKeyDown(JumpButton))
        {
            rb.velocity = new Vector3(0, JumpHeight, 0);
        }
        if(Input.GetKey(MoveRightButton))
        {
            rb.velocity = new Vector3(MoveSpeed, 0, 0);
        }
        if(Input.GetKey(MoveLeftButton))
        {
            rb.velocity = new Vector3(MoveSpeed*-1, 0, 0);
        }
    }
}

You should try using the KeyCode type instead of the string type for Unity input to avoid issues like this. I would suggest briefly reading:

https://docs.unity3d.com/ScriptReference/KeyCode.html

to learn more about the different options available. In the Unity inspector for any object with that Movement script on it you'll then be able to assign the button you wish to use with a dropdown.

Unity processes enums in a user-friendly way such that you are able to view them in the inspector. Strings can not achieve this same functionality.

Here is some updated source code for you:

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

[RequireComponent(typeof(Rigidbody))]
public class Movement : MonoBehaviour
{
    [Header("Movement Preferences")]
    public int JumpHeight;
    public int MoveSpeed;

    [Header("Key Assignments")]
    public KeyCode JumpButton;
    public KeyCode MoveRightButton;
    public KeyCode MoveLeftButton;

    private Rigidbody2D rb;

    // Start is called before the first frame update
    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    private void Update()
    {
        if (Input.GetKeyDown(JumpButton))
            rb.velocity = new Vector3(0, JumpHeight, 0);

        if (Input.GetKey(MoveRightButton))
            rb.velocity = new Vector3(MoveSpeed, 0, 0);

        if (Input.GetKey(MoveLeftButton))
            rb.velocity = new Vector3(MoveSpeed*-1, 0, 0);
    }
}

The GetKey method does not work with string, you have to set the keycode. Remember that the key is different from the button in Unity, and if you want to use a string, use the GetButton alternative.

public KeyCode jumpKey;
public KeyCode upKey;

public void Update()
{
    if (Input.GetKeyDown(jumpKey)) { } // do jump

    if (Input.GetKey(upKey)) { } // move Up

    if (Input.GetKeyDown(KeyCode.Space)) { } // when space pressed
    
}

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM