Shift+W 时的不同动作?它一直只是进入 "W" 分支,而不是让我专门编码 Shift+W
Different movements upon Shift+W? It keeps just entering the "W" branch and not letting me exclusively code Shift+W
好的,所以我正在尝试让不同的玩家动画和速度在玩家按下 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 时不同?
您需要切换检查密钥的方式。
GetKeyDown 仅适用于您按下按键的帧 (https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html), and GetKey remains true while the key is held down (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);
}
好的,所以我正在尝试让不同的玩家动画和速度在玩家按下 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 时不同?
您需要切换检查密钥的方式。 GetKeyDown 仅适用于您按下按键的帧 (https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html), and GetKey remains true while the key is held down (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);
}