帧速率独立的pushForce? [英] Frame-rate independent pushForce?

查看:152
本文介绍了帧速率独立的pushForce?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CharacterController,并增加了推动刚体的功能.然而,问题在于推送是取决于帧速率的.如何使它独立于帧速率?我曾尝试添加Time.deltatime,但这无法进行推送,尽管可能添加有误.

I'm working with a CharacterController and added the ability to push Rigidbodies. The problem is however that the pushing is frame-rate dependent. How would I be able to make it frame-rate independent? I have tried adding Time.deltatime, but this makes pushing not possible, I might be adding it wrong though.

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

Here's the code that adds force to rigidbodies;

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;
}

据我所知,它与最后两行代码有关.

As far as I know it has something to do with the last 2 lines of code.

编辑(用于推送的代码):

Edit(The code for pushing):

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;
    }
} 

推荐答案

您是对的,您需要乘以Time.deltaTime,这是自上次渲染帧以来经过的时间. 由于此数字确实很小(您的帧速率为60+),因此还需要增加该值(分别乘以100、1000 ...)

You were right, you need to multiply by Time.deltaTime which is the time elapsed since last rendered frame. As this number is really small (has you have 60+ fps) you'll also need to increase the value (multiply it by 100, 1000...)

这篇关于帧速率独立的pushForce?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆