getKeyDown 按键不再有效,之前有效。调试也不输出

getKeyDown key presses no longer working, worked before. Debug also not outputting

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

    Vector3 curPos = transform.position;

    // after mouse has been left clicked and user presses spacebar or up arrow keys to carry out animations.
    if (Input.GetKeyDown(KeyCode.Mouse0))
    {
        if (Input.GetMouseButtonDown(0))
        {

            //defining targetPos as the mouse click position
            Vector3 targetPos = Input.mousePosition;
            //outputting to console target position
            Debug.Log("Mouse Position " + targetPos);

            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                //newPos is the position of where miffy is meant to go.
                newPos = new Vector3(hit.point.x, 0.1199974f, hit.point.z);
                //setting isMiffyMoving to true to carry out miffyMove() actions
                isMiffyMoving = true;
            }
            //if statements to detmine miffy's direction when shes moving and output to console.
            if (targetPos.x < curPos.x)
            {
                Debug.Log("Miffy is going left");
                left = true;
                isIdle = false;
            }
            else if (targetPos.x > curPos.x)
            {
                Debug.Log("Miffy is going right");
                left = false;
                isIdle = false;
            }

            if (targetPos.z < curPos.z)
            {
                Debug.Log("Miffy is going backward");
                forward = false;
                isIdle = false;
            }
            else if (targetPos.z > curPos.z)
            {
                Debug.Log("Miffy is going forward");
                forward = true;
                isIdle = false;
            }
            //setting isIdle to true is miffy is not moving
            if (isIdle)
            {
                Debug.Log("Miffy is idle");
                isIdle = true;
            }

            ////when up arrow key has been pressed animation stored in SpinJump will play
            if (Input.GetKeyDown(KeyCode.UpArrow))
            {
               anim.Play("SpinJump");
                Debug.Log("Working");
            }

            ////if spacebar has been pressed animation stored in cheer with play

            if (Input.GetKeyDown(KeyCode.Space))
            {
                anim.Play("cheer");
                Debug.Log("Working");
            }

        }

这是我的代码,按键不起作用,"working"输出只有在鼠标键按下时才有效。

我试过将按键放入它们自己的方法中,并在按下鼠标 0 后在更新中调用它们。

在我添加一个将角色移动到鼠标点击的移动方法之前,它是这样工作的

它不起作用,因为您在检查鼠标按钮单击的代码中保留了 space 按钮的条件。只有当您按下鼠标时,debug.log 才会提供工作输出然后在不抬起鼠标的情况下同时点击 space。

你必须这样改:

void Update()
{

    Vector3 curPos = transform.position;

    // after mouse has been left clicked and user presses spacebar or up arrow keys to carry out animations.
    if (Input.GetKeyDown(KeyCode.Mouse0))
    {
        if (Input.GetMouseButtonDown(0))
        {

            //defining targetPos as the mouse click position
            Vector3 targetPos = Input.mousePosition;
            //outputting to console target position
            Debug.Log("Mouse Position " + targetPos);

            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                //newPos is the position of where miffy is meant to go.
                newPos = new Vector3(hit.point.x, 0.1199974f, hit.point.z);
                //setting isMiffyMoving to true to carry out miffyMove() actions
                isMiffyMoving = true;
            }
            //if statements to detmine miffy's direction when shes moving and output to console.
            if (targetPos.x < curPos.x)
            {
                Debug.Log("Miffy is going left");
                left = true;
                isIdle = false;
            }
            else if (targetPos.x > curPos.x)
            {
                Debug.Log("Miffy is going right");
                left = false;
                isIdle = false;
            }

            if (targetPos.z < curPos.z)
            {
                Debug.Log("Miffy is going backward");
                forward = false;
                isIdle = false;
            }
            else if (targetPos.z > curPos.z)
            {
                Debug.Log("Miffy is going forward");
                forward = true;
                isIdle = false;
            }
            //setting isIdle to true is miffy is not moving
            if (isIdle)
            {
                Debug.Log("Miffy is idle");
                isIdle = true;
            }
        }

    }

    ////when up arrow key has been pressed animation stored in SpinJump will play
    if (Input.GetKeyDown(KeyCode.UpArrow))
    {
        anim.Play("SpinJump");
        Debug.Log("Working");
    }

    ////if spacebar has been pressed animation stored in cheer with play

    if (Input.GetKeyDown(KeyCode.Space))
    {
        anim.Play("cheer");
        Debug.Log("Working");
    }
}

使用这个你的代码可以正常工作,你得到你想要的。

我不得不从鼠标点击中删除 GetKeyDowns 但仍在更新中。我添加了 isIdle = false。有太多的动作试图同时发生导致紧张。

//if up arrow is pressed  animation stored in Spin will play
    if (Input.GetKeyDown(KeyCode.UpArrow))
    {
        //setting idle to false to ensure it will not execute while spin is executing.
        isIdle = false;

        //setting cheer to false to ensure it will not execute while spin is executing.
        cheer = false;

        //setting spin to true.
        spin = true;
        skip = false;

        // playing animation for miffy to spin.
        anim.Play("SpinJump", 0, 0f);

        //testing if input is working by outputting text "working" to console.
        Debug.Log("Working");
    }

    ////if space has been pressed animation stored in Cheer with play
    if (Input.GetKeyDown(KeyCode.Space))
    {
        //setting idle to false to ensure it will not execute while cheer is executing.
        isIdle = false;

        //setting spin to false to ensure it will not execute while cheer is executing.
        spin = false;

        //setting cheer to true.
        cheer = true;
        skip = false;

        // playing animation for miffy to cheer.
        anim.Play("Cheer", 0, 0f);

        //testing if input is working by outputting text "working" to console.
        Debug.Log("Working");

    }