简体   繁体   English

使用角色控制器将玩家的速度保持在斜坡上

[英]Keep player velocity on slopes using Character Controller

I am using Unity's Character Controller tool to move my character. 我正在使用Unity的角色控制器工具移动角色。

I am using my own movement, gravity, etc, but it seems to be not affecting directly to the movement. 我正在使用自己的运动,重力等,但这似乎并没有直接影响运动。 I've checked my whole code and reduced it to the minimum just to seek the problem. 我检查了我的整个代码,并将其减少到最低限度,以解决问题。

I've printed velocity, movement, magnitudes... Everything shows up that the movement it's exactly the same being on a flat surface or on slopes. 我已经打印了速度,运动,幅度...一切都表明,在平坦表面或斜坡上的运动完全相同。

I don't know how the Character Controller's script works, so a kind of calculation might being causing that behaviour. 我不知道角色控制器的脚本是如何工作的,因此可能是某种计算引起的行为。

This is how I calculate my movement: 这是我计算运动的方式:

private void SetMovement() {
    horizontalMovement = isometricHorizontal * Input.GetAxisRaw(horizontalButton);
    verticalMovement = isometricVertical * Input.GetAxisRaw(verticalButton);
    movement = (horizontalMovement + verticalMovement).normalized;
    MovementModifier();
}

private void MovementModifier() {
    if (isSprinting) {
        movement.x *= sprintFactor;
        movement.z *= sprintFactor;
    }
}

private void Move() {
    if (movement != Vector3.zero)
        characterController.Move(movement * Time.deltaTime * moveSpeed);
}

The velocity down slopes increases at least 30% and is being reduced almost the same when going up. 速度下降斜率至少增加30%,上升时减小的速度几乎相同。

I would like to keep the same velocity on any surface without needing to apply a movement modificator or reducing it while being over a certain area like slopes or stairs. 我想在任何表面上保持相同的速度,而无需在地面或楼梯等特定区域上应用运动调节器或降低它。

EDIT: 编辑:

This is how I apply gravity. 这就是我施加重力的方式。

private void ApplyGravity() {
    if (!characterController.isGrounded) {
        gravity += Physics.gravity * Time.deltaTime * gravityFactor;
        if (gravity.y > 0 && !Input.GetButton(jumpButton) && !isFalling)
            gravity += Physics.gravity * Time.deltaTime * gravityFactor * (lowJumpMultiplier - 1);
    }
    else if (!isJumping) gravity = Vector3.down;
    CheckCollisionFlags();
    movement += gravity;
}

private void CheckCollisionFlags() {
    switch (characterController.collisionFlags) {
        case UnityEngine.CollisionFlags.Above:
            gravity = Vector3.down;
            break;  
    }
}

Without seeing how you implement gravity, I can't be 100% sure but I would assume that something like this is happening ( treating it as if you were making a 2D side scroller ): 在没有看到您如何实现重力的情况下,我不能百分百确定,但我会假设正在发生类似的事情( 将其视为您正在制作2D侧面滚动条 ):

Your character would have horizontal movement, and as that is normalized you would get a constant horizontal speed. 您的角色将水平移动,并且按照正常化操作,您将获得恒定的水平速度。

However when you apply gravity, you are accelerating vertically, causing the velocity downwards to increase. 但是,当您施加重力时,您将垂直加速,从而导致向下的速度增加。

As you go down a slope, your horizontal movement is constant, but your vertical movement caused by gravity is getting added to your velocity after your velocity was normalized, resulting in a larger velocity when on a slope. 在斜坡上时,水平运动是恒定的,但是对速度进行归一化 ,重力引起的垂直运动会添加到速度中,从而在倾斜时会产生较大的速度。


To get around this, you would probably need to do something like this: 为了解决这个问题,您可能需要执行以下操作:

Check if you are grounded, if you are then you are moving on a flat surface or on a slope, and you should normalize your velocity after adding the vertical movement caused by gravity. 检查是否已接地,如果要接地,则要在平坦的表面或斜坡上移动,在加上重力引起的垂直移动后,应归一化速度。

I've just find out that I had to increase the CharacterController's Step Offset property. 我发现我不得不增加CharacterController的Step Offset属性。 It seems that it was making some conflict while going up slopes or stairs and deducting the movement. 似乎在上坡或上楼梯并扣减运动时发生了一些冲突。

I achieved now the same movement speed going up and down slopes, that is higher than in flat surfaces, but it is easier to manage so far. 现在,我实现了相同的上下移动速度,该速度比在平坦表面上要快,但到目前为止,它更易于管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM