如果玩家最初碰撞,跳跃应该只是直线上升

时间:2018-03-16 19:36:34

标签: c# unity3d

您好,我目前正在使用第三人控制器,并且在玩家发生碰撞时遇到跳跃问题。目前,当玩家与立方体碰撞但用户试图向立方体方向移动时,当玩家不再碰撞时(Midair),玩家将朝着用户最初试图前进的方向前进。我想要它,以便当用户在碰撞时跳跃,即使玩家不再在较高点碰撞,玩家也只会直线上升。我怎么做到这一点?我尝试过不同的东西,比如使用光线投射和碰撞器,但遗憾的是没有运气。

我现在拥有的相关脚本:

void Move(Vector2 inputDir, bool running)
{
    if (inputDir != Vector2.zero)
    {
        float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraT.eulerAngles.y;
        transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, GetModifiedSmoothTime(turnSmoothTime));
    }

    float targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;
    currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime));

    velocityY += Time.deltaTime * gravity;

    Vector3 velocity = transform.forward * currentSpeed + Vector3.up * velocityY;
    controller.Move(velocity * Time.deltaTime);

    currentSpeed = new Vector2(controller.velocity.x, controller.velocity.z).magnitude;

    // Checks if player is not grounded and is falling

    if (GroundCheck())
    {
        velocityY = 0;
        anim.SetBool("onAir", false);
    }
    else
    {
        anim.SetBool("onAir", true);
    }
}

void JumpWalk()
{
    if (JumpCheck())
    {
        float jumpVelocity = Mathf.Sqrt(-2 * gravity * jumpHeightIdle);
        velocityY = jumpVelocity;
        anim.SetTrigger("JumpWalk");
        canJump = Time.time + 1.3f; //Delay after jump input
    }
}

1 个答案:

答案 0 :(得分:0)

有些注意事项:

  • 玩家控制器根据用户输入控制的速度变量移动(翻译)你的角色。
  • 玩家控制器正在检测与墙壁的碰撞/交叉,并将角色移离墙壁,使其不再相交。
  • 玩家控制器不知道速度变量,因此无法改变速度(仅限位置/平移)。
  • 您没有明确处理与墙壁的碰撞,因此您不会因与墙壁碰撞而调整速度。
  • 当玩家在空中但不再接触墙壁时(最可能的是,对角线滑向墙壁),它会根据速度移动而不会受到墙壁的干扰。

因此,玩家角色恢复其原始速度,因为没有更新该变量。

您需要使用void OnCollision(Collision collision)方法手动处理碰撞,并根据碰撞数据更新速度变量。

这将要求你的角色拥有RigidBody,但是为了防止物理引擎与你的玩家控制器的移动(翻译)竞争,该身体将需要运动。