Клавиши getKeyDown больше не работают, работали раньше. Отладка тоже не выводится - PullRequest
0 голосов
/ 24 января 2020
// 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");
            }

        }

Это мой код, нажатия клавиш не будут работать, а «рабочий» вывод работает только тогда, когда он находится вне клавиши мыши.

Я пытался поместить нажатия клавиш в свои собственные метод и вызов их в обновлении после нажатия мыши 0.

Так было до того, как я добавил метод перемещения, который перемещает символ к щелчку мыши

Ответы [ 3 ]

1 голос
/ 24 января 2020

Вы должны измениться так:

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");
    }
}

Используя этот код, ваш код работает правильно, и вы получаете именно то, что вы хотите.

0 голосов
/ 28 января 2020

Мне пришлось удалить 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");

    }
0 голосов
/ 24 января 2020

это не работает, потому что вы сохранили условие для кнопки пробела внутри кодировки, где вы проверяете наличие нажатия кнопки мыши ..., что debug.log даст рабочий результат, только если вы нажмете кнопку мыши, а затем нажмете пробел одновременно не отрывая мышки.

...