简体   繁体   English

当我使用要按下的按钮设置变量时,Unity C#GetKey() 不起作用

[英]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.它不适用于箭头键,我在 GetKey() 键码上找不到太多信息。 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这是我的代码://脚本名称是“Movement”,在对象上附加了一个刚体2D,重力为1.5,盒子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.您应该尝试对 Unity 输入使用KeyCode类型而不是string类型,以避免出现此类问题。 I would suggest briefly reading:我建议简要阅读:

https://docs.unity3d.com/ScriptReference/KeyCode.html 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 检查器中,对于任何带有该移动脚本的对象,您将能够通过下拉菜单分配您希望使用的按钮。

Unity processes enums in a user-friendly way such that you are able to view them in the inspector. Unity 以用户友好的方式处理枚举,以便您能够在检查器中查看它们。 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. GetKey方法不适用于字符串,您必须设置键码。 Remember that the key is different from the button in Unity, and if you want to use a string, use the GetButton alternative.请记住,键与 Unity 中的按钮不同,如果要使用字符串,请使用GetButton替代方案。

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
    
}

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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