如何使用新输入法输入字符运行?
How to make character run using the new input system?
我正在尝试为我的角色控制器运动添加 运行ning 功能。当 Z key
被按住时,我希望角色为 运行。我正在使用新的输入系统,这是我的尝试:
Vector3 horizontalVelocity;
[SerializeField] CharacterController controller;
[SerializeField] float walkSpeed = 10f;
[SerializeField] float runSpeed = 25f;
bool running;
private void Update()
{
if (running)
{
moveSpeed = runSpeed;
horizontalVelocity = (transform.right * horizontalInput.x + transform.forward * horizontalInput.y) * moveSpeed;
controller.Move(horizontalVelocity * Time.deltaTime);
}
else
{
moveSpeed = walkSpeed;
horizontalVelocity = (transform.right * horizontalInput.x + transform.forward * horizontalInput.y) * moveSpeed;
controller.Move(horizontalVelocity * Time.deltaTime);
}
}
public void OnRunning() {
running = true;
}
我在另一个 class 中处理了输入操作,如下所示:
[SerializeField] Movement movement;
PlayerControls controls;
PlayerControls.GroundMovementActions groundMovement;
private void Awake()
{
groundMovement.Running.performed += _ => movement.OnRunning();
}
private void OnEnable()
{
controls.Enable();
}
private void OnDestroy()
{
controls.Disable();
}
没用,按不按z都正常走。当我按住 z key
.
时不会 运行
首先,将密钥Action Type
更改为Button
:
使 运行 变量成为 public。现在直接从按键控制代码中,当按下Z键时,使运行为true,如果取消,则将其设置为false。
public bool running;
_controls.GamePlay.Running.performed += _ = movement.running = true;
_controls.GamePlay.Running.canceled += _ = movement.running = false;
我正在尝试为我的角色控制器运动添加 运行ning 功能。当 Z key
被按住时,我希望角色为 运行。我正在使用新的输入系统,这是我的尝试:
Vector3 horizontalVelocity;
[SerializeField] CharacterController controller;
[SerializeField] float walkSpeed = 10f;
[SerializeField] float runSpeed = 25f;
bool running;
private void Update()
{
if (running)
{
moveSpeed = runSpeed;
horizontalVelocity = (transform.right * horizontalInput.x + transform.forward * horizontalInput.y) * moveSpeed;
controller.Move(horizontalVelocity * Time.deltaTime);
}
else
{
moveSpeed = walkSpeed;
horizontalVelocity = (transform.right * horizontalInput.x + transform.forward * horizontalInput.y) * moveSpeed;
controller.Move(horizontalVelocity * Time.deltaTime);
}
}
public void OnRunning() {
running = true;
}
我在另一个 class 中处理了输入操作,如下所示:
[SerializeField] Movement movement;
PlayerControls controls;
PlayerControls.GroundMovementActions groundMovement;
private void Awake()
{
groundMovement.Running.performed += _ => movement.OnRunning();
}
private void OnEnable()
{
controls.Enable();
}
private void OnDestroy()
{
controls.Disable();
}
没用,按不按z都正常走。当我按住 z key
.
首先,将密钥Action Type
更改为Button
:
使 运行 变量成为 public。现在直接从按键控制代码中,当按下Z键时,使运行为true,如果取消,则将其设置为false。
public bool running;
_controls.GamePlay.Running.performed += _ = movement.running = true;
_controls.GamePlay.Running.canceled += _ = movement.running = false;