【发布时间】: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 键的自定义键盘真是太棒了。一个人可以做梦。