예제 #1
0
        ///// Methods /////

        /// <summary>
        ///
        /// </summary>
        public void ProcessMovement(ISurfControllable surfer, MovementConfig config, float deltaTime)
        {
            // cache instead of passing around parameters
            _surfer    = surfer;
            _config    = config;
            _deltaTime = deltaTime;

            // apply gravity
            if (_surfer.GroundObject == null)
            {
                _surfer.MoveData.Velocity.y -= (_surfer.MoveData.GravityFactor * _config.Gravity * _deltaTime);
                _surfer.MoveData.Velocity.y += _surfer.BaseVelocity.y * _deltaTime;
            }

            // input velocity, check for ground
            CheckGrounded();
            CalculateMovementVelocity();

            // increment origin
            _surfer.MoveData.Origin += _surfer.MoveData.Velocity * _deltaTime;

            // don't penetrate walls
            SurfPhysics.ResolveCollisions(_surfer.Collider, ref _surfer.MoveData.Origin, ref _surfer.MoveData.Velocity);

            _surfer = null;
        }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        public void ProcessMovement(ISurfControllable surfer, MovementConfig config, float deltaTime)
        {
            // cache instead of passing around parameters
            _surfer    = surfer;
            _config    = config;
            _deltaTime = deltaTime;

            if (!_surfer.moveData.underwater)
            {
                if (_surfer.moveData.velocity.y <= 0f)
                {
                    jumping = false;
                }

                // apply gravity
                if (_surfer.groundObject == null)
                {
                    _surfer.moveData.velocity.y -= (_surfer.moveData.gravityFactor * _config.gravity * _deltaTime);
                    _surfer.moveData.velocity.y += _surfer.baseVelocity.y * _deltaTime;
                }

                // input velocity, check for ground
                CheckGrounded();
                CalculateMovementVelocity();
            }
            else
            {
                // Do underwater logic
                UnderwaterPhysics();
            }

            float yVel = _surfer.moveData.velocity.y;

            _surfer.moveData.velocity.y = 0f;
            _surfer.moveData.velocity   = Vector3.ClampMagnitude(_surfer.moveData.velocity, _config.maxVelocity);
            speed = _surfer.moveData.velocity.magnitude;
            _surfer.moveData.velocity.y = yVel;

            float   maxDistPerFrame   = 1f;
            Vector3 velocityThisFrame = _surfer.moveData.velocity * _deltaTime;
            float   velocityDistLeft  = velocityThisFrame.magnitude;
            float   initialVel        = velocityDistLeft;

            while (velocityDistLeft > 0f)
            {
                float amountThisLoop = Mathf.Min(maxDistPerFrame, velocityDistLeft);
                velocityDistLeft -= amountThisLoop;

                // increment origin
                Vector3 velThisLoop = velocityThisFrame * (amountThisLoop / initialVel);
                _surfer.moveData.origin += velThisLoop;

                // don't penetrate walls
                SurfPhysics.ResolveCollisions(_surfer.collider, ref _surfer.moveData.origin, ref _surfer.moveData.velocity, _surfer.moveData.rigidbodyPushForce);
            }

            _surfer.moveData.groundedTemp = _surfer.moveData.grounded;

            _surfer = null;
        }
예제 #3
0
        public void CalculateMovement(ISurfControllable surfer, MovementConfig config, float deltaTime)
        {
            // cache instead of passing around parameters
            _surfer    = surfer;
            _config    = config;
            _deltaTime = deltaTime;

            _surfer.MoveData.JustGrounded = false;
            _surfer.MoveData.JustJumped   = false;

            if (surfer.MoveData.LimitedExecution)
            {
                CalculateSyncMovement();
            }
            else
            {
                CheckParameters();
                CalculateFullMovement();
            }

            _surfer = null;
            _config = null;
        }
        public void Crouch(ISurfControllable surfer, MovementConfig config, float deltaTime)
        {
            _surfer    = surfer;
            _config    = config;
            _deltaTime = deltaTime;

            if (_surfer == null)
            {
                return;
            }

            if (_surfer.collider == null)
            {
                return;
            }

            bool grounded      = _surfer.groundObject != null;
            bool wantsToCrouch = _surfer.moveData.crouching;

            float crouchingHeight  = Mathf.Clamp(_surfer.moveData.crouchingHeight, 0.01f, 1f);
            float heightDifference = _surfer.moveData.defaultHeight - _surfer.moveData.defaultHeight * crouchingHeight;

            if (grounded)
            {
                uncrouchDown = false;
            }

            // Crouching input
            if (grounded)
            {
                crouchLerp = Mathf.Lerp(crouchLerp, wantsToCrouch ? 1f : 0f,
                                        _deltaTime * _surfer.moveData.crouchingSpeed);
            }
            else if (!grounded && !wantsToCrouch && crouchLerp < 0.95f)
            {
                crouchLerp = 0f;
            }
            else if (!grounded && wantsToCrouch)
            {
                crouchLerp = 1f;
            }

            // Collider and position changing
            if (crouchLerp > 0.9f && !crouching)
            {
                // Begin crouching
                crouching = true;
                if (_surfer.collider.GetType() == typeof(BoxCollider))
                {
                    // Box collider
                    BoxCollider boxCollider = (BoxCollider)_surfer.collider;
                    boxCollider.size = new Vector3(boxCollider.size.x, _surfer.moveData.defaultHeight * crouchingHeight,
                                                   boxCollider.size.z);
                }
                else if (_surfer.collider.GetType() == typeof(CapsuleCollider))
                {
                    // Capsule collider
                    CapsuleCollider capsuleCollider = (CapsuleCollider)_surfer.collider;
                    capsuleCollider.height = _surfer.moveData.defaultHeight * crouchingHeight;
                }

                // Move position and stuff
                _surfer.moveData.origin += heightDifference / 2 * (grounded ? Vector3.down : Vector3.up);
                foreach (Transform child in playerTransform)
                {
                    if (child == _surfer.moveData.viewTransform)
                    {
                        continue;
                    }

                    child.localPosition = new Vector3(child.localPosition.x, child.localPosition.y * crouchingHeight,
                                                      child.localPosition.z);
                }

                uncrouchDown = !grounded;
            }
            else if (crouching)
            {
                // Check if the player can uncrouch
                bool canUncrouch = true;
                if (_surfer.collider.GetType() == typeof(BoxCollider))
                {
                    // Box collider
                    BoxCollider boxCollider = (BoxCollider)_surfer.collider;
                    Vector3     halfExtents = boxCollider.size * 0.5f;
                    Vector3     startPos    = boxCollider.transform.position;
                    Vector3     endPos      = boxCollider.transform.position +
                                              (uncrouchDown ? Vector3.down : Vector3.up) * heightDifference;

                    Trace trace = Tracer.TraceBox(startPos, endPos, halfExtents, boxCollider.contactOffset,
                                                  SurfPhysics.groundLayerMask);

                    if (trace.hitCollider != null)
                    {
                        canUncrouch = false;
                    }
                }
                else if (_surfer.collider.GetType() == typeof(CapsuleCollider))
                {
                    // Capsule collider
                    CapsuleCollider capsuleCollider = (CapsuleCollider)_surfer.collider;
                    Vector3         point1          = capsuleCollider.center + Vector3.up * capsuleCollider.height * 0.5f;
                    Vector3         point2          = capsuleCollider.center + Vector3.down * capsuleCollider.height * 0.5f;
                    Vector3         startPos        = capsuleCollider.transform.position;
                    Vector3         endPos          = capsuleCollider.transform.position +
                                                      (uncrouchDown ? Vector3.down : Vector3.up) * heightDifference;

                    Trace trace = Tracer.TraceCapsule(point1, point2, capsuleCollider.radius, startPos, endPos,
                                                      capsuleCollider.contactOffset, SurfPhysics.groundLayerMask);

                    if (trace.hitCollider != null)
                    {
                        canUncrouch = false;
                    }
                }

                // Uncrouch
                if (canUncrouch && crouchLerp <= 0.9f)
                {
                    crouching = false;
                    if (_surfer.collider.GetType() == typeof(BoxCollider))
                    {
                        // Box collider
                        BoxCollider boxCollider = (BoxCollider)_surfer.collider;
                        boxCollider.size = new Vector3(boxCollider.size.x, _surfer.moveData.defaultHeight,
                                                       boxCollider.size.z);
                    }
                    else if (_surfer.collider.GetType() == typeof(CapsuleCollider))
                    {
                        // Capsule collider
                        CapsuleCollider capsuleCollider = (CapsuleCollider)_surfer.collider;
                        capsuleCollider.height = _surfer.moveData.defaultHeight;
                    }

                    // Move position and stuff
                    _surfer.moveData.origin += heightDifference / 2 * (uncrouchDown ? Vector3.down : Vector3.up);
                    foreach (Transform child in playerTransform)
                    {
                        child.localPosition = new Vector3(child.localPosition.x,
                                                          child.localPosition.y / crouchingHeight, child.localPosition.z);
                    }
                }

                if (!canUncrouch)
                {
                    crouchLerp = 1f;
                }
            }

            // Changing camera position
            if (!crouching)
            {
                _surfer.moveData.viewTransform.localPosition = Vector3.Lerp(
                    _surfer.moveData.viewTransformDefaultLocalPos,
                    _surfer.moveData.viewTransformDefaultLocalPos * crouchingHeight +
                    Vector3.down * heightDifference * 0.5f, crouchLerp);
            }
            else
            {
                _surfer.moveData.viewTransform.localPosition = Vector3.Lerp(
                    _surfer.moveData.viewTransformDefaultLocalPos - Vector3.down * heightDifference * 0.5f,
                    _surfer.moveData.viewTransformDefaultLocalPos * crouchingHeight, crouchLerp);
            }
        }
예제 #5
0
        /// <summary>
        ///
        /// </summary>
        public void ProcessMovement(ISurfControllable surfer, MovementConfig config, float deltaTime)
        {
            // cache instead of passing around parameters
            _surfer    = surfer;
            _config    = config;
            _deltaTime = deltaTime;

            if (_surfer.moveData.laddersEnabled && !_surfer.moveData.climbingLadder)
            {
                // Look for ladders
                LadderCheck(new Vector3(1f, 0.95f, 1f), _surfer.moveData.velocity * Mathf.Clamp(Time.deltaTime * 2f, 0.025f, 0.25f));
            }

            if (_surfer.moveData.laddersEnabled && _surfer.moveData.climbingLadder)
            {
                LadderPhysics();
            }
            else if (!_surfer.moveData.underwater)
            {
                if (_surfer.moveData.velocity.y <= 0f)
                {
                    jumping = false;
                }

                // apply gravity
                if (_surfer.groundObject == null)
                {
                    _surfer.moveData.velocity.y -= (_surfer.moveData.gravityFactor * _config.gravity * _deltaTime);
                    _surfer.moveData.velocity.y += _surfer.baseVelocity.y * _deltaTime;
                }

                // input velocity, check for ground
                CheckGrounded();
                CalculateMovementVelocity();
            }
            else
            {
                // Do underwater logic
                UnderwaterPhysics();
            }

            float yVel = _surfer.moveData.velocity.y;

            _surfer.moveData.velocity.y = 0f;
            _surfer.moveData.velocity   = Vector3.ClampMagnitude(_surfer.moveData.velocity, _config.maxVelocity);
            speed = _surfer.moveData.velocity.magnitude;
            _surfer.moveData.velocity.y = yVel;

            if (_surfer.moveData.velocity.sqrMagnitude == 0f)
            {
                // Do collisions while standing still
                SurfPhysics.ResolveCollisions(_surfer.collider, ref _surfer.moveData.origin, ref _surfer.moveData.velocity, _surfer.moveData.rigidbodyPushForce, _surfer.collider.transform.localPosition, 1f, _surfer.moveData.stepOffset, _surfer);
            }
            else
            {
                float   maxDistPerFrame   = 0.2f;
                Vector3 velocityThisFrame = _surfer.moveData.velocity * _deltaTime;
                float   velocityDistLeft  = velocityThisFrame.magnitude;
                float   initialVel        = velocityDistLeft;
                while (velocityDistLeft > 0f)
                {
                    float amountThisLoop = Mathf.Min(maxDistPerFrame, velocityDistLeft);
                    velocityDistLeft -= amountThisLoop;

                    // increment origin
                    Vector3 velThisLoop = velocityThisFrame * (amountThisLoop / initialVel);
                    _surfer.moveData.origin += velThisLoop;

                    // don't penetrate walls
                    SurfPhysics.ResolveCollisions(_surfer.collider, ref _surfer.moveData.origin, ref _surfer.moveData.velocity, _surfer.moveData.rigidbodyPushForce, _surfer.collider.transform.localPosition, amountThisLoop / initialVel, _surfer.moveData.stepOffset, _surfer);
                }
            }

            _surfer.moveData.groundedTemp = _surfer.moveData.grounded;

            _surfer = null;
        }