代码之家  ›  专栏  ›  技术社区  ›  Rachel Dockter

输入GetKeyDown未触发

  •  1
  • Rachel Dockter  · 技术社区  · 6 年前

    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标签,但它是用于二维Unity游戏。

    1 回复  |  直到 6 年前
        1
  •  2
  •   Hamid Yusifli    6 年前

    您的代码似乎都很好,因此控制台窗口可能有错误。确保在控制台的右上角突出显示了注释框,以便在代码中打印内容时,它将出现。另外,我建议你使用力量让你的角色跳跃而不是速度。在这种情况下,你说玩家的垂直速度 jumpHeight 但千万不要改变它,这样你的角色就会不断向上移动。您应该将该行更改为:

    playerBody.AddForce (transform.up * jumpHeight);
    

    你可能需要调整 跳高 因此。