【问题标题】:Why is unity not recognising my left shift key为什么统一无法识别我的左移键
【发布时间】:2021-05-18 20:02:26
【问题描述】:

所以我有一个简单的空对象,附有 3d 角色控制器。这是我的玩家移动脚本的更新功能

// Do the ground check
isGrounded = Physics.CheckSphere(groundCheck.position, groundDist, groundMask);
if (isGrounded && yMovement.y < 0)
{
    yMovement.y = -2; // reset the gravity velocity
}
    
// get the xz movement of the player
float xMovement = Input.GetAxis("Horizontal");
float zMovement = Input.GetAxis("Vertical");
Vector3 xzMovement = transform.right * xMovement + transform.forward * zMovement;

// Check for sprinting
if (Input.GetButtonDown("Sprint"))
{
    xzMovement *= sprintMultiplier;
}
    
// Check if the player should jump
if (Input.GetButtonDown("Jump") && isGrounded)
{
    yMovement.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
}

// move the player on xz
controller.Move(xzMovement * speed * Time.deltaTime);

// move the player on y
yMovement.y += gravity * Time.deltaTime;
controller.Move(yMovement * Time.deltaTime);

在冲刺检查期间,无论是否按下按钮,“xzMovement *= sprintingMultiplier”都不会被调用。这是为什么呢?

顺便说一下,在输入sprint中设置了一个“左移”的正按钮。

非常感谢任何帮助。

【问题讨论】:

  • 很确定您想使用Input.GetKeyDown 进行键盘输入。它有一个字符串参数的重载。所以只需使用Input.GetKeyDown("Sprint"))。按钮用于鼠标/控制器输入按钮。可以提供更多细节here
  • @TEEBQNE 实际上不是。 GetKeyDown(string) 指的是实际的键名,如"a""space""left shift" 等。如果 OP 配置了一个名为 "Sprint" 的虚拟按钮,它可以有各种触发器,例如键盘上的某个键、操纵杆或控制器按钮等,或者正如 OP 所说的正面键盘输入"left shift"Input.GetKeyDown("Sprint") 永远不会起作用,因为(至少在我的键盘上:P)没有这样的键称为 Sprint ;)
  • @derHugo 很有趣很高兴知道 - 谢谢!拥有一个带有 sprint 键的自定义键盘真是太棒了。一个人可以做梦。

标签: c# unity3d


【解决方案1】:

您想使用Input.GetButton 而不是Input.GetButtonDown

GetButtonDown 仅在按下按钮时单帧返回 true,而 GetButton 在按钮保持按下状态时每一帧返回 true。

【讨论】:

  • 从 OPs 代码看来,这些代码只打算被调用一次
  • @derHugo 我想他明白了。通常冲刺是通过按住 shift、ctrl 或其他来激活的。通过仅在按下键时在帧中设置移动来很好地处理跳跃,但仅在第一次按下键时乘以冲刺速度似乎并不正确。我不知道transform 在这种情况下是什么,但xzMovement 可能是这个框架的移动增量,除非乘数很大,否则乘以一次不会很明显。
  • @MarkSouls 哦,是的,对不起!我非常专注于 Jump 部分,以至于我没有意识到 OP 实际上询问了 Sprint 部分
  • GetButton 看起来像我正在寻找的东西。谢谢!
  • 很高兴我能帮上忙! :D 感谢@derHugo 的编辑!
猜你喜欢
  • 2016-06-03
  • 2020-01-16
  • 2019-02-17
  • 1970-01-01
  • 2017-02-12
  • 2021-12-25
  • 1970-01-01
  • 2019-09-02
  • 2020-09-04
相关资源
最近更新 更多