简体   繁体   English

我的推动机制取决于帧速率

[英]My pushing mechanic is frame-rate dependent

I am having issues with my CharacterController interacting with rigidbodies. 我的CharacterController与刚体互动时遇到问题。 It is kind off working, but only at 60 fps and gives strange behaviour when I turn of v-sync or the frame-rate is low/veryhigh. 它工作正常,但只能以60 fps播放,当我进行垂直同步或帧率过低或过高时,会产生奇怪的行为。 I have some code for the interaction between the CharacterController and rigidbodies and I have created a function called PushStates, which is loaded in the Update function. 我有一些用于CharacterController和僵尸之间交互的代码,并且创建了一个名为PushStates的函数,该函数已加载到Update函数中。

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 * Time.deltaTime;
}

Here I have some code relevant to my pushing mechanics. 在这里,我有一些与推动技巧有关的代码。 Another thing that is not yet working is removing all the forces from the rigidbody when the player stops moving. 尚不起作用的另一件事是,当播放器停止移动时,将所有力从刚体上移开。 This causes the pushableObject to still move away from the player for like a couple of cm, which I'm also not sure about why it's not working. 这会导致pushableObject仍然从播放器移开几厘米,我也不确定为什么它不起作用。

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并更改检查器中的某些设置,即(pushForce,pushSpeed)解决了我的问题。

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

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