【问题标题】:Input getKeyDown not firing输入 getKeyDown 不触发
【发布时间】:2018-05-28 03:44:26
【问题描述】:

我正在使用 Unity 制作 2d 游戏。我有一个附有这个脚本的玩家精灵。左右键移动完美,但是当我尝试获取空格键时,它不起作用

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

    public class Player : MonoBehaviour {

        public float moveSpeed;
        public float jumpHeight;

        // Use this for initialization
        void Start () {

        }

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

            Rigidbody2D playerBody = GetComponent<Rigidbody2D>();

            if (Input.GetKeyDown("space")){
                print("fired");
                //playerBody.velocity = new Vector2(playerBody.velocity.x, jumpHeight);
            }

            if (Input.GetKey(KeyCode.LeftArrow))
            {
                playerBody.velocity = new Vector2(-moveSpeed, playerBody.velocity.y);
            }

            if (Input.GetKey(KeyCode.RightArrow))
            {
                playerBody.velocity = new Vector2(moveSpeed, playerBody.velocity.y);
            }
        }
    }

我也尝试过Keycode.Space,而不是“空格”,这也不起作用

另外由于某种原因,我无法删除或替换 unity3d 标签,但它是用于 2d unity 游戏的。

【问题讨论】:

  • Unity 使用Debug.Log 将消息打印到 Unity 调试控制台。
  • 打印也适合我

标签: c# unity3d


【解决方案1】:

您的代码似乎一切正常,因此您的控制台窗口可能有错误。确保在控制台的右上角突出显示了注释框,这样每当您在代码中打印某些内容时,它就会出现。另外,我建议你使用力量来让你的角色跳跃而不是速度。在这种情况下,您将玩家的垂直速度设为jumpHeight,但永远不要将其改回,这样您的角色将不断向上移动。您应该将该行更改为:

playerBody.AddForce (transform.up * jumpHeight);

您可能需要相应地调整jumpHeight

【讨论】:

  • 谢谢,我很确定这只是我的空格键很奇怪,因为如果我将空格更改为另一个键它可以工作。无论如何我都会接受这个答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-17
  • 2010-10-22
相关资源
最近更新 更多