Esempio n. 1
0
    void UpdateMoveGround(float dt)
    {
        var rotOfPlane    = Quaternion.FromToRotation(OnGround.up, Vector3.up);
        var revRotOfPlane = Quaternion.FromToRotation(Vector3.up, OnGround.up);

        // movement in the Y axis (jump), Grounded && space in the last few frames?
        if (grounded && space.Contains((v) => v))
        {
            SnapToGround(maxDistToFloor);
            OnGround.details.jumping.Record(true);
            OnGround.details.groundTouches.Wash(false);


            var relVel   = rotOfPlane * myBody.velocity;
            var relVelXY = new Vector3(relVel.x, 0.0f, relVel.z);
            myBody.velocity = revRotOfPlane * relVelXY;

            myBody.AddForce(transform.up * OnGround.jumpForce);
        }

        // apply friction
        {
            myBody.velocity = myBody.velocity - (myBody.velocity * OnGround.friction * dt);
        }


        // apply move force to rigid body
        float redirectForce = CalcRedirectForceMultiplier(OnGround.redirectForce);

        ApplyMoveForce(OnGround.moveForce * redirectForce, dt, OnGround.up);

        // Clamp Velocity along horizontal plane
        {
            var   maxSpeed       = OnGround.maxSpeed;
            var   relVel         = rotOfPlane * myBody.velocity;
            var   relVelXY       = new Vector3(relVel.x, 0.0f, relVel.z);
            var   relVelXYNorm   = relVelXY.normalized;
            float horzontalSpeed = relVelXY.magnitude;

            float newSpeed = maxSpeed; // Mathf.Lerp(horzontalSpeed, maxSpeed, 0.8f * dt);
            if (horzontalSpeed > maxSpeed)
            {
                Vector3 newVel = new Vector3(relVelXYNorm.x * newSpeed, relVel.y, relVelXYNorm.z * newSpeed);
                myBody.velocity = revRotOfPlane * newVel;
            }
        }

        // Rotate based on mouse look
        if (cameraController.lookVec.x != 0)
        {
            float turnAmount = cameraController.lookVec.x;
            var   rotation   = Quaternion.AngleAxis(turnAmount, Vector3.up);
            transform.localRotation = transform.localRotation * rotation;
        }
    }