Ejemplo n.º 1
0
        private void FixedUpdate()
        {
            //Update ground state
            UpdateGround();
            if (!mGrounded)
            {
                mTimeSinceGround += Time.fixedDeltaTime;
            }

            //[Gafgar: Sat/01-02-2020]: feels like we should be able to have a less special case for this.. or handle this in the grounding function?
            if (GroundingBlockTimer > 0)
            {
                GroundingBlockTimer -= Time.fixedDeltaTime;
                if (GroundingBlockTimer < 0)
                {
                    GroundingBlockTimer = 0;
                }
            }

            mState.StateUpddateFixed(this);

            //get positions for collision test before the move
            Vector3 bottomSphere = Ctrl.transform.position + new Vector3(0, Ctrl.height * -0.5f + Ctrl.radius + 0.1f, 0);

            if (mGrounded)
            {
                bottomSphere.y += Ctrl.stepOffset + Ctrl.radius * 0.5f; //move up from the ground so we don't count small steps as blocking
            }
            Vector3 topSphere = Ctrl.transform.position + new Vector3(0, Ctrl.height * 0.5f + Ctrl.radius - 0.1f, 0);

            //perform the move
            Ctrl.Move((mVelocity + mMoveOffset) * Time.fixedDeltaTime);
            mMoveOffset = Vector3.zero;

            //look for walls and react to them (don't react to if we move in to stuff, as this has more control and it's enough to react to one hit per frame
            RaycastHit hit;

            if (Physics.CapsuleCast(bottomSphere, topSphere, Ctrl.radius, mVelocity.normalized, out hit, mVelocity.magnitude * Time.fixedDeltaTime * 3, ~LayerMask.NameToLayer("Items"), QueryTriggerInteraction.Ignore))
            {
                // Debug.Log("hit :" + hit.collider.gameObject.name + ": normal: " + hit.normal);
                if (hit.rigidbody && !hit.rigidbody.isKinematic)
                {
                    hit.rigidbody.AddForceAtPosition(mVelocity * 200.7f, hit.point, ForceMode.Force);
                }
                if (hit.normal.y < 0.75f)//not ground?
                {
                    Vector3 n = hit.normal;
                    if (hit.normal.y > -0.1f)
                    {
                        n.y *= 0.7f; //don't block the downward or upward velocity completely by reducing the normal size
                    }
                    float d = Vector3.Dot(mVelocity, n);
                    if (d < 0)
                    {
                        mVelocity -= n * d; //[Gafgar: Sat/01-02-2020]: consider only removing a % here, to make it easier to move around corners
                    }
                }
            }

            if (mCarryingComponent)
            {
                if (!mHeighlightingConnectionj)
                {
                    float distance     = 12;
                    float heightOffset = 0.3f;
                    distance = mCarryingComponent._colliderExt.magnitude + 0.3f;
                    Vector3 up = Cam.transform.up;

                    float speedMod = Mathf.Min(1.0f, 0.2f + mCarryTime * 0.8f);

                    heightOffset = 1.0f - up.y * 0.5f;

                    mCarryPosition += ((Cam.transform.position + Cam.transform.forward * distance - up * heightOffset) - mCarryPosition) * 50.0f * speedMod * Time.fixedDeltaTime;
                    mCarryRotation  = Quaternion.Slerp(mCarryRotation, Cam.transform.rotation, 12.0f * speedMod * Time.fixedDeltaTime);
                    mCarryingComponent.transform.position = mCarryPosition;
                    mCarryingComponent.transform.rotation = mCarryRotation;

                    if (mCarryTime < 1.5f)
                    {
                        mCarryTime += Time.fixedDeltaTime;
                    }
                }
                else
                {
                    if (mCarryTime > 0.01f)
                    {
                        if (mCarryTime > 0.9f)
                        {
                            mCarryTime = 0.9f;
                        }
                        mCarryTime -= Time.fixedDeltaTime * 1.5f;
                        if (mCarryTime < 0.01f)
                        {
                            mCarryTime = 0.01f;
                        }
                    }

                    mCarryPosition += ((mHeighlightingConnectionj._TranformProxy.transform.position + mHeighlightingConnectionj._TranformProxy.transform.up * 0.5f) - mCarryPosition) * 4.0f * Time.fixedDeltaTime;
                    mCarryRotation  = Quaternion.Slerp(mCarryRotation, mHeighlightingConnectionj._TranformProxy.transform.rotation, 6.0f * Time.fixedDeltaTime);
                    mCarryingComponent.transform.position = mCarryPosition;
                    mCarryingComponent.transform.rotation = mCarryRotation;
                }
            }

            mVelocitySoft      += (mVelocity - mVelocitySoft) * 3.0f * Time.fixedDeltaTime;
            mHeadBobOffsetSoft += (mHeadBobOffset - mHeadBobOffsetSoft) * 9.0f * Time.fixedDeltaTime;
            mHeadBobOffset     -= mHeadBobOffset * 6.0f * Time.fixedDeltaTime;
            //handle leaning in acceleration feedback
            {
                var pos  = Vector3.zero;
                var diff = (mVelocity - mVelocitySoft) * 0.05f;
                diff.y *= 0.5f;
                diff    = Vector3.ClampMagnitude(diff, 2.4f);

                if (mGrounded)
                {
                    pos.x = diff.x * 0.4f;
                    pos.z = diff.z * 0.4f;
                }
                else
                {
                    pos.x = Mathf.Clamp(mVelocitySoft.x * 0.04f, -2.0f, 2.0f) * Mathf.Clamp(mVelocity.y * 0.1f, -1.0f, 1.0f);
                    pos.z = Mathf.Clamp(mVelocitySoft.z * 0.04f, -2.0f, 2.0f) * Mathf.Clamp(mVelocity.y * 0.1f, -1.0f, 1.0f);
                }
                pos.y     = (diff.y * (mGrounded ? -5.0f : -0.5f)) - (diff.x * diff.x) * 0.6f - (diff.z * diff.z) * 0.6f;
                mLeanPos += (pos - mLeanPos) * 8.0f * Time.fixedDeltaTime;
            }

            mLastGronuded = mGrounded;
        }