Beispiel #1
0
        /// <summary>
        /// On Update we we lock the door if needed
        /// </summary>
        protected virtual void Update()
        {
            _currentDirection.x = Door.transform.right.x;
            _currentDirection.y = Door.transform.right.z;
            float Angle = MMMaths.AngleBetween(_initialDirection, _currentDirection);

            if ((Angle > MinAngle) && (Angle < MaxAngle) && (!Door.isKinematic))
            {
                if (Angle > 180)
                {
                    Door.transform.localRotation = Quaternion.Euler(_maxAngleRotation);
                }
                else
                {
                    Door.transform.localRotation = Quaternion.Euler(_minAngleRotation);
                }
                Door.transform.position     = _initialPosition;
                Door.collisionDetectionMode = CollisionDetectionMode.Discrete;
                Door.isKinematic            = true;
            }

            // if enough time has passed we reset our door
            if ((Time.time - _lastContactTimestamp > SafeLockDuration) && (Door.isKinematic))
            {
                Door.isKinematic = false;
            }
        }
        /// <summary>
        /// Handles the existing forces on a character when entering/exiting a zone
        /// </summary>
        /// <param name="entering">If set to <c>true</c> entering.</param>
        /// <param name="gravityDirection">Gravity direction.</param>
        protected virtual void Transition(bool entering, Vector2 gravityDirection)
        {
            float gravityAngle = 180 - MMMaths.AngleBetween(Vector2.up, gravityDirection);

            if (TransitionForcesMode == TransitionForcesModes.Nothing)
            {
                return;
            }
            if (TransitionForcesMode == TransitionForcesModes.Reset)
            {
                _controller.SetForce(Vector2.zero);
                _movement.ChangeState(CharacterStates.MovementStates.Idle);
            }
            if (TransitionForcesMode == TransitionForcesModes.Adapt)
            {
                // the angle is calculated depending on if the player enters or exits a zone and takes _previousGravityAngle as parameter if you glide over from one zone to another
                float rotationAngle = entering ? _previousGravityAngle - gravityAngle : gravityAngle - _defaultGravityAngle;

                _controller.SetForce(Quaternion.Euler(0, 0, rotationAngle) * _controller.Speed);
            }
            if (ResetCharacterStateOnGravityChange)
            {
                if (_movement.CurrentState == CharacterStates.MovementStates.Dashing)
                {
                    _character.gameObject.GetComponentNoAlloc <CharacterDash>().StopDash();
                }
                _movement.ChangeState(CharacterStates.MovementStates.Idle);
            }
            _previousGravityAngle = entering ? gravityAngle : _defaultGravityAngle;
        }
Beispiel #3
0
        /// <summary>
        /// Rotates the cone, interpolating the rotation if needed
        /// </summary>
        /// <param name="direction"></param>
        protected virtual void AimAt(Vector3 direction)
        {
            if (Interpolate)
            {
                _newAim = MMMaths.Lerp(_newAim, direction, InterpolateRate, Time.deltaTime);
            }
            else
            {
                _newAim = direction;
            }

            _angle         = MMMaths.AngleBetween(this.transform.right, _newAim);
            _eulerAngles.y = -_angle;

            _coneOfVision2D.SetDirectionAndAngles(_newAim, _eulerAngles);
        }
        /// <summary>
        /// Finds the closest gravity point and changes the gravity if needed
        /// </summary>
        protected virtual void ComputeGravityPoints()
        {
            // if we're not affected by gravity points, we do nothing and exit
            if (!SubjectToGravityPoints)
            {
                return;
            }
            // if we're in a gravity zone, we do nothing and exit
            if (_inAGravityZone)
            {
                return;
            }

            // we grab the closest gravity point
            _closestGravityPoint = GetClosestGravityPoint();

            // if it's not null
            if (_closestGravityPoint != null)
            {
                // our new gravity point becomes the closest if we didn't have one already, otherwise we stay on the last gravity point met for now
                _newGravityPoint = (_lastGravityPoint == null) ? _closestGravityPoint : _lastGravityPoint;
                // if we've got a new gravity point
                if ((_lastGravityPoint != _closestGravityPoint) && (_lastGravityPoint != null))
                {
                    // if we haven't entered a new gravity point in a while, we switch to that new gravity point
                    if (Time.time - _entryTimeStampPoints >= InactiveBufferDuration)
                    {
                        _entryTimeStampPoints = Time.time;
                        _newGravityPoint      = _closestGravityPoint;
                        Transition(true, _newGravityPoint.transform.position - _controller.ColliderCenterPosition);
                        StartRotating();
                    }
                }
                // if we didn't have a gravity point last time, we switch to this new one
                if (_lastGravityPoint == null)
                {
                    if (Time.time - _entryTimeStampPoints >= InactiveBufferDuration)
                    {
                        _entryTimeStampPoints = Time.time;
                        _newGravityPoint      = _closestGravityPoint;
                        Transition(true, _newGravityPoint.transform.position - _controller.ColliderCenterPosition);
                        StartRotating();
                    }
                }
                // we override our gravity
                _gravityPointDirection = _newGravityPoint.transform.position - _controller.ColliderCenterPosition;
                float gravityAngle = 180 - MMMaths.AngleBetween(Vector2.up, _gravityPointDirection);
                _gravityOverridden    = true;
                _overrideGravityAngle = gravityAngle;
                _lastGravityPoint     = _newGravityPoint;
            }
            else
            {
                // if we don't have a gravity point in range, our gravity is not overridden
                if (Time.time - _entryTimeStampPoints >= InactiveBufferDuration)
                {
                    if (_lastGravityPoint != null)
                    {
                        Transition(false, _newGravityPoint.transform.position - _controller.ColliderCenterPosition);
                        StartRotating();
                    }
                    _entryTimeStampPoints = Time.time;
                    _gravityOverridden    = false;
                    _lastGravityPoint     = null;
                }
            }
        }
        protected virtual void ComputeGravityPoints()
        {
            // if not affected by gravity ponts, do nothing and exit
            if (!subjectToGravityPoints)
            {
                return;
            }

            // grab closest gravity point
            _closestGravityPoint = GetClosestGravityPoint();
            Debug.Log(_closestGravityPoint.transform.position);

            // if it's not null
            if (_closestGravityPoint != null)
            {
                _newGravityPoint = (_lastGravityPoint == null) ? _closestGravityPoint : _lastGravityPoint;
                // if we have a new gravity point
                if ((_lastGravityPoint != _closestGravityPoint) && (_lastGravityPoint != null))
                {
                    // time check to switch gravity point
                    if (Time.time - _entryTimeStampPoints >= InactiveBufferDuration)
                    {
                        _entryTimeStampPoints = Time.time;
                        _newGravityPoint      = _closestGravityPoint;
                        Transition(true, _newGravityPoint.transform.position - this.transform.position);
                        // StartRotating();
                    }
                }
                // if we didn't have a gravity point last time
                if (_lastGravityPoint == null)
                {
                    // time check to switch gravity point
                    if (Time.time - _entryTimeStampPoints >= InactiveBufferDuration)
                    {
                        _entryTimeStampPoints = Time.time;
                        _newGravityPoint      = _closestGravityPoint;
                        Transition(true, _newGravityPoint.transform.position - this.transform.position);
                        // StartRotating();
                    }
                }
                // ovveride gravity
                _gravityPointDirection = _newGravityPoint.transform.position - this.transform.position;
                float gravityAngle = 180 - MMMaths.AngleBetween(Vector2.up, _gravityPointDirection);
                _gravityOverridden    = true;
                _overrideGravityAngle = gravityAngle;
                _lastGravityPoint     = _newGravityPoint;
            }
            else
            {
                // without gravity point in range, our gravity is not overridden
                if (Time.time - _entryTimeStampPoints >= InactiveBufferDuration)
                {
                    if (_lastGravityPoint != null)
                    {
                        Transition(false, _newGravityPoint.transform.position - this.transform.position);
                        // StartRotating();
                    }
                    _entryTimeStampPoints = Time.time;
                    _gravityOverridden    = false;
                    _lastGravityPoint     = null;
                }
            }
        }