Различные движения по Shift + W?Он продолжает просто входить в ветку "W" и не позволяет мне использовать только код Shift + W - PullRequest
0 голосов
/ 26 мая 2019

ОК, поэтому я пытаюсь сделать так, чтобы анимация и скорость другого игрока происходили, когда игрок нажимал Shift + W, а не просто W.

Вот рабочий код только для W:

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

public class MherControls : MonoBehaviour
{

    float speed = 2;
    float rotSpeed = 80;
    float rot = 0f; //0 when we start the game
    float gravity = 8;

    Vector3 moveDir = Vector3.zero;

    CharacterController controller;
    Animator anim;

    // Start is called before the first frame update
    void Start()
    {
        controller = GetComponent<CharacterController>();
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        //anim condition 0 = idle, 1 = walk, 2 = run

        if (controller.isGrounded)
        {
            if (Input.GetKey(KeyCode.W))
            {
                anim.SetInteger("condition", 1); //changes condition in Animator Controller to 1

                moveDir = new Vector3(0, 0, 1); //only move on the zed axis
                moveDir *= speed;                 

                moveDir = transform.TransformDirection(moveDir); 


                if (speed < 10){
                    speed += Time.deltaTime; //max speed is 10
                    //Debug.Log(speed);
                }
                if (speed >= 2.5)
                {
                    anim.SetInteger("condition", 2);
                }



            }
            if (Input.GetKeyUp(KeyCode.W))
            {
                anim.SetInteger("condition", 0); 
                speed = 2;
                moveDir = new Vector3(0, 0, 0); 
            }



            rot += Input.GetAxis("Horizontal") * rotSpeed * Time.deltaTime; //horizontal are A and D keys and also left and right arrows
            transform.eulerAngles = new Vector3(0, rot, 0); //our character's transform property

        }
            //every frame move player on y axis by 8. so lowering to the ground
            moveDir.y -= gravity * Time.deltaTime;
            controller.Move(moveDir * Time.deltaTime);
    }
}

Однако, когда я пытаюсь представить поведение Shift + W, пример:

if ( (Input.GetKey(KeyCode.W)) && (Input.GetKeyDown(KeyCode.LeftShift)) {
      speed = 2;
      anim.SetInteger("condition", 1);
}

Тогда это не работает. Он просто продолжает входить в ветку W и никогда не позволяет мне кодировать поведение исключительно для Shift + W.

Что я делаю не так? Как я могу вести себя иначе, когда игрок держит Shift + W, что отличается от того, когда игрок держит только W?

1 Ответ

1 голос
/ 26 мая 2019

Вам необходимо изменить способ проверки ключей.GetKeyDown имеет значение true только для кадра, на котором вы нажимаете клавишу (https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html),, а GetKey остается истинным, пока клавиша удерживается нажатой (https://docs.unity3d.com/ScriptReference/Input.GetKey.html).). Чтобы удерживать Shift, а затем нажать W, проверка должна быть

if ( (Input.GetKey(KeyCode.LeftShift)) && (Input.GetKeyDown(KeyCode.W)) {
      speed = 2;
      anim.SetInteger("condition", 1);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...