【问题标题】:Frame-rate independent pushForce?与帧率无关的 pushForce?
【发布时间】:2019-02-21 13:05:29
【问题描述】:

我正在使用 CharacterController 并添加了推送刚体的功能。然而,问题在于推送取决于帧速率。我怎样才能使它与帧速率无关?我曾尝试添加 Time.deltatime,但这使得无法推送,不过我可能添加错了。

这是为刚体增加力的代码;

void OnControllerColliderHit(ControllerColliderHit hit)
{
    Rigidbody body = hit.collider.attachedRigidbody;

    if (body == null || body.isKinematic)
        return;

    if (hit.moveDirection.y < -.3f)
        return;

    Vector3 pushDirection = new Vector3(hit.moveDirection.x, 0, hit.moveDirection.z);
    body.velocity = pushForce * pushDirection;
}

据我所知,这与最后两行代码有关。

编辑(推送代码):

public void PushStates() {
    // Creating the raycast origin Vector3's
    Vector3 forward = transform.TransformDirection(Vector3.forward) * distanceForPush;
    Vector3 middle = controller.transform.position - new Vector3(0, -controller.height / 2, 0);

    // Inspector bool
    if (pushRay)
    {
        Debug.DrawRay(middle, forward, Color.cyan);
    }

    // Force the pushForce and movementSpeed to normal when the player is not pushing
    pushForce = 0f;
    movementSpeed = walkSpeed;

    // Draws a raycast in front of the player to check if the object in front of the player is a pushable object
    if (Physics.Raycast(middle, forward, out hit, distanceForPush))
    {
        if (InputManager.BButton() && playerIsInPushingTrigger)
        {
            PushableInfo();

            playerIsPushing = true;
            anim.SetBool("isPushing", true);

            if (hit.collider.tag == "PushableLight")
            {
                pushForce = playerPushForceLight;
                movementSpeed = pushSpeedLight;
            }
            else if (hit.collider.tag == "PushableHeavy")
            {
                pushForce = playerPushForceHeavy;
                movementSpeed = pushSpeedHeavy;
            }

            // Checks the players speed now instead off movement. This is neccesary when the player is pushing a pushable into a collider. 
            // The player and pushable never stop moving because of force.
            if (currentSpeed < 0.15f)
            {
                //Removes all remaining velocity, when the player stops pushing
                pushableObjectRB.velocity = Vector3.zero;
                pushableObjectRB.angularVelocity = Vector3.zero;

                anim.SetFloat("pushSpeedAnim", 0f);
            }
            else
            {
                // Calls a rotation method
                PushingRot();
                if (hit.collider.tag == "PushableLight")
                {
                    anim.SetFloat("pushSpeedAnim", pushSpeedAnimLight);
                }
                else if (hit.collider.tag == "PushableHeavy")
                {
                    anim.SetFloat("pushSpeedAnim", pushSpeedAnimHeavy);
                }
            }
        }
        else
        {
            anim.SetBool("isPushing", false);
            pushForce = 0f;
            movementSpeed = walkSpeed;
            playerIsPushing = false;
        }
    }
    else
    {
        anim.SetBool("isPushing", false);
        playerIsPushing = false;
    }

    // Setting the time it takes to rotate when pushing
    AnimatorStateInfo stateInfo = anim.GetCurrentAnimatorStateInfo(0);
    if (stateInfo.fullPathHash == pushStateHash)
    {
        turnSmoothTime = maxTurnSmoothTimePushing;
    }
    else
    {
        turnSmoothTime = 0.1f;
    }
} 

【问题讨论】:

    标签: unity3d game-physics rigid-bodies


    【解决方案1】:

    你是对的,你需要乘以 Time.deltaTime,这是自上次渲染帧以来经过的时间。 由于这个数字非常小(您是否有 60+ fps),您还需要增加该值(将其乘以 100、1000...)

    【讨论】:

    • 你的意思是我需要增加pushForce?如果我可以问的话,我乘以 Time.deltaTime 怎么样?
    • 只做body.velocity = pushForce * pushDirection * Time.deltaTime * someValueToAdjust 但是,你不是在更新、循环或任何东西中做的,所以它不应该首先依赖于帧率?
    • someValueToAdjust 是什么?而且确实这不是直接在update函数中,但是当CharacterController与刚体碰撞时,这不就意味着它会自动在Update中运行吗?
    • 啊,我认为要调整的值是 * 100 对吗?我只测试了几秒钟,但它似乎有效。然而,在禁用垂直同步后,由于某种原因,它似乎仍然依赖于帧速率?
    • multiply it by 100, 1000...) 或多或少60 不够吗? ;)
    猜你喜欢
    • 1970-01-01
    • 2014-09-03
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    相关资源
    最近更新 更多