【问题标题】:Unity C# - Move character while jumpingUnity C# - 跳跃时移动角色
【发布时间】:2019-05-20 06:01:52
【问题描述】:

我的角色动作很好,跳跃也很好。但是在跳跃时,他只是朝着他来的方向直线移动,在空中你不能旋转或移动他。怎么可能?

来自更新函数:

if (controller.isGrounded)
{
    moveD = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
    moveD = transform.TransformDirection(moveD.normalized) * speed;
    moveDA = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));

    if (moveDA.magnitude > 0)
    {                 
        gameObject.transform.GetChild(0).LookAt(gameObject.transform.position + moveDA, Vector3.up);
    }

    if (Input.GetButton("Jump"))
    {
        moveD.y = jumpSpeed;
    }
}

moveD.y = moveD.y - (gravity * Time.deltaTime);
controller.Move(moveD * Time.deltaTime);

【问题讨论】:

  • 你需要将你的移动、旋转和跳跃逻辑分开。现在他们都在一起了,你的答案就在第一行:if...isGrounded。

标签: c# unity3d character move


【解决方案1】:

controller.isGrounded 仅当您最后一次调用controller.Move() 时对象的对撞机的底部接触到表面时才为真,因此在您的情况下,一旦您跳跃,您将无法移动,直到再次撞到地面。

你可以像这样分离你的移动代码和跳跃代码来解决这个问题:

moveD = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
moveD = transform.TransformDirection(moveD.normalized) * speed;
moveDA = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));

if (moveDA.magnitude > 0) 
{ 
  gameObject.transform.GetChild(0).LookAt(gameObject.transform.position + moveDA, Vector3.up);
}

if (controller.isGrounded)
{
  if (Input.GetButton("Jump"))
  {
    moveD.y = jumpSpeed;
  }
}
moveD.y = moveD.y - (gravity * Time.deltaTime);
controller.Move(moveD * Time.deltaTime);

【讨论】:

  • 感谢您的回复 - 现在跳跃时移动有效,我需要使用归一化,这样角色在对角线移动时不会走得更快(即上限幅度为
  • @JackNapier 此处发布的代码 JLum 应该可以做到这一点。请把这些问题变成一个新问题,以便我们更好地讨论您的代码。
猜你喜欢
  • 2017-12-22
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-17
  • 1970-01-01
相关资源
最近更新 更多