Exemplo n.º 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;
        }
Exemplo n.º 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;
        }
Exemplo n.º 3
0
        private void IncrementOrigin(Vector3 amount)
        {
            _surfer.MoveData.PreviousOrigin = _surfer.MoveData.Origin;
            _surfer.MoveData.Origin        += amount;

            if (_surfer.MoveType == MoveType.Noclip && !_config.NoclipCollide)
            {
                return;
            }

            SurfPhysics.ResolveCollisions(_surfer);

            var prevOrigin       = _surfer.MoveData.PreviousOrigin;
            var newOrigin        = _surfer.MoveData.Origin;
            var movementThisStep = newOrigin - prevOrigin;
            var newMovement      = movementThisStep;

            if (movementThisStep.magnitude >= _surfer.Collider.bounds.extents.x)
            {
                var center = prevOrigin;
                center.y += _surfer.Collider.bounds.extents.y;

                var hitCount = Physics.BoxCastNonAlloc(center: center,
                                                       halfExtents: _surfer.Collider.bounds.extents * 0.5f,
                                                       direction: movementThisStep.normalized,
                                                       orientation: Quaternion.identity,
                                                       results: _hitCache,
                                                       maxDistance: movementThisStep.magnitude,
                                                       layerMask: SurfPhysics.GroundLayerMask,
                                                       queryTriggerInteraction: QueryTriggerInteraction.Ignore);

                if (hitCount > 0)
                {
                    for (int i = 0; i < hitCount; i++)
                    {
                        newMovement += _hitCache[i].normal * (movementThisStep.magnitude - _hitCache[i].distance);
                        SurfPhysics.ClipVelocity(_surfer.MoveData.Velocity, _hitCache[i].normal, ref _surfer.MoveData.Velocity, 1.01f);
                    }
                    _surfer.MoveData.Origin = prevOrigin + newMovement;
                }
            }
        }
Exemplo n.º 4
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;
        }