如何引用"SpaceBar"?键入C#(Unity)? [英] How to Reference the "SpaceBar" Key in C# (Unity)?

查看:108
本文介绍了如何引用"SpaceBar"?键入C#(Unity)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Unity中,如何在按下空格键时引用空格键以对对象施加力?

In Unity, how do I reference the SpaceBar Key in order to apply force to an object when Space Bar is pressed?

void Update()
{
    rb.AddForce(0, 0, forwardforce * Time.deltaTime);

    if (Input.GetKey("a"))
    {
        rb.AddForce(500 * Time.deltaTime, 0, 0);
    }
    if (Input.GetKey("w"))
    {
        rb.AddForce(0, 0, -500 * Time.deltaTime);
    }
    if (Input.GetKey("s"))
    {
        rb.AddForce(0, 0, 500 * Time.deltaTime);
    }
    if (Input.GetKey("d"))
    {
        rb.AddForce(-500 * Time.deltaTime, 0, 0);
    }
    if (Input.GetKey("vbKeySpace"))
    {
        rb.AddForce(0, 300 * Time.deltaTime, 0);
    }
}

推荐答案

您可以尝试以下操作:

Input.GetKey("space");

或者,使用KeyCode枚举:

Or, using the KeyCode enumeration:

Input.GetKey(KeyCode.Space);

在此枚举中查看文档此处.

这篇关于如何引用"SpaceBar"?键入C#(Unity)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆