如何使角色在团结跳跃时移动

时间:2018-11-10 18:05:12

标签: c# unity3d

这是我当前拥有的代码,但是我希望能够进行中间跳跃。 从现在开始,如果我在移动时跳跃,我将继续朝那个方向前进,但是当我在空中时,我无法改变前进的方向。此代码是使用统一代码制作的,此代码附加到的播放器是胶囊。

private CharacterController charController;

[SerializeField] private AnimationCurve jumpFallOff;
[SerializeField] private float jumpMultipliyer;
[SerializeField] private KeyCode jumpKey;

private bool isJumping;

private void Awake()
{
    charController = GetComponent<CharacterController>();
}

private void Update()
{
    PlayerMovement();
}

private void PlayerMovement()
{
    float horziInput = Input.GetAxis(horizontalInputName) * movementSpeed;
    float vertInput = Input.GetAxis(verticalInputName) * movementSpeed;

    Vector3 forwardMovement = transform.forward * vertInput;
    Vector3 rightMovement = transform.right * horziInput;

    charController.SimpleMove(forwardMovement + rightMovement);

    JumpInput();
}

private void JumpInput()
{
    if(Input.GetKeyDown(jumpKey) && !isJumping)
    {
        isJumping = true;
        StartCoroutine(JumpEvent());
    }
}

private IEnumerator JumpEvent()
{
    charController.slopeLimit = 90.0f;
    float timeInAir = 0.0f;

    do
    {
        float jumpForce = jumpFallOff.Evaluate(timeInAir);
        charController.Move(Vector3.up * jumpForce * jumpMultipliyer * Time.deltaTime);
        timeInAir += Time.deltaTime;

        yield return null;

    } while (!charController.isGrounded && charController.collisionFlags != CollisionFlags.Above);

    charController.slopeLimit = 45.0f;
    isJumping = false;
}

0 个答案:

没有答案