墙跳高度随着每次跳跃而降低

时间:2015-05-30 04:53:26

标签: c# unity3d

已解决 - 我最终删除playerController.SimpleMove并让playerController.Move控制所有玩家的移动。

所以我一直试图让我的角色墙在墙壁之间跳跃。我已经能够使它工作,但是,每次成功的墙跳后,下一跳的高度会降低。这就到了墙壁向下跳跃的程度。我不知道为什么会这样做。在每次按空格键之前,我将运动Vector3重置为零,然后将正确的值重新应用于跳转。我甚至已经通过控制台查看垂直更改,并且moveDirection和moveAmount都没有获得足够大的值更改以实现此目的。

因为我只是尝试这个,所以下面是影响播放器的所有代码。

public float rotateSpeed = 3.0f;
public float walkSpeed = 5.0f;
public float runSpeed = 15.0f;
public float jumpSpeed = 10.0f;
public float acceleration = .05f;

public bool running = false;
public bool jumping = false;
public bool falling = false;
public bool onWall = false;
public bool wallJumping = false;
public bool stay = false;

private CharacterController playerController;
private Animator playerAnimator;
private float speed;
private Vector3 moveAmount;
private float animationSpeed;

private float currentSpeed;
private float targetSpeed;
private float moveSpeed;

float gravity = 10f;
private Vector3 moveDirection = Vector3.zero;


// Use this for initialization
void Start () {
    playerController = GetComponent<CharacterController>();
    playerAnimator = GetComponent<Animator>();
}

// Update is called once per frame
void Update () {
    transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
if (onWall)
    {
        playerAnimator.SetBool("Wall Holding", true);
        jumping = false;
    }
    else
    {
        playerAnimator.SetBool("Wall Holding", false);
    }

    if (stay)
    {
        moveDirection = Vector3.zero;
        moveAmount = Vector3.zero;
    }

    #region Player Controls
    if (Input.GetButton("Vertical") && !wallJumping)
    {
        moveSpeed = (Input.GetKey(KeyCode.LeftShift)) ? runSpeed : walkSpeed;
        moveAmount = transform.TransformDirection(Vector3.forward);
    }
    else
    {
        moveSpeed = 0;
        moveAmount = Vector3.zero;
    }

    if (playerController.isGrounded){
        moveDirection = Vector3.zero;
        jumping = false;
        wallJumping = false;
        onWall = false;
        stay = false;
    }
    else
    {
        if (!stay)
            onWall = false;
    }

    if (Input.GetKeyDown(KeyCode.Space))
    {
        if (playerController.isGrounded)
        {
            moveDirection.y = jumpSpeed;
            jumping = true;
        }
        else if (onWall)
        {
            moveDirection = Vector3.zero;
            moveAmount = Vector3.zero;
            wallJumping = true;
            moveDirection += transform.forward * jumpSpeed * 2f;
            moveDirection += transform.up * jumpSpeed * 2f;
            stay = false;
        }
    }

    if (Input.GetKeyDown(KeyCode.F))
    {
        if (playerAnimator.GetBool("fight"))
        {
            playerAnimator.SetBool("fight", false);
        }
        else
        {
            playerAnimator.SetBool("fight", true);
        }
    }
    #endregion Player Controls

    moveDirection.y -= gravity * Time.deltaTime;
    Debug.Log(moveDirection.x);
    targetSpeed = Input.GetAxis("Vertical") * moveSpeed;
    currentSpeed = SpeedFactor(targetSpeed, currentSpeed, acceleration);

    if (currentSpeed > walkSpeed)
        running = true;
    else
        running = false;

    playerAnimator.SetFloat("movespeed", currentSpeed);

    if (!stay)
    {
        playerController.SimpleMove(currentSpeed * moveAmount);
        playerController.Move(moveDirection * Time.deltaTime);
    }
}

private float SpeedFactor (float targetSpeed, float currentSpeed, float dilation) {
    if ( currentSpeed < targetSpeed) {
        currentSpeed += dilation;
        if ( currentSpeed > targetSpeed) {
            currentSpeed = targetSpeed;
        }
    }
    else if ( currentSpeed > targetSpeed) {
        currentSpeed -= dilation;
        if ( currentSpeed < targetSpeed) {
            currentSpeed = targetSpeed;
        }
    }
    return currentSpeed;
}

void OnControllerColliderHit (ControllerColliderHit hit) {
    if (hit.gameObject.tag == "JumpingWall" && !playerController.isGrounded && !onWall && (jumping || wallJumping))
    {
        onWall = true;
        transform.Rotate(0,180,0);
        stay = true;
    }
}

1 个答案:

答案 0 :(得分:0)

最简单的解释是transform.forward和/或transform.up向量随着时间的推移开始偏离您的期望。这可以解释当你应用下面的行时,它甚至似乎最终会“向后”跳跃。在跳线与墙壁发生碰撞(下一次跳跃之前)后,你确定这些矢量是按照你的预期定向的吗?

moveDirection += transform.forward * jumpSpeed * 2f;
moveDirection += transform.up * jumpSpeed * 2f;