简体   繁体   中英

My pushing mechanic is frame-rate dependent

I am having issues with my CharacterController interacting with rigidbodies. 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. 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.

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.

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)解决了我的问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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