Пример #1
0
        /// <summary>
        /// Instantiates the hit effect.
        /// </summary>
        public void OnHitTarget(Hit hit)
        {
            instantiate(Hit, hit.Position);

            if (PerformPauses)
            {
                HitPauseManager.Pause(hit.HitPauseDuration, hit.HitPauseDelay);
            }
        }
Пример #2
0
        /// <summary>
        /// Does the camera follow logic.
        /// </summary>
        private void LateUpdate()
        {
            // Updated in many places just in case.
            HitPauseManager.Update();

            if (Target == null)
            {
                return;
            }

            var angle = 90f;
            var motor = Target.GetComponent <CharacterMotor>();

            if (motor != null)
            {
                angle = motor.NextAngle;
            }

            _orientation = Quaternion.Lerp(_orientation, Quaternion.AngleAxis(angle - 90, Vector3.up), Time.deltaTime * TurnSpeed);

            Zoom = Mathf.Lerp(Zoom, _zoomTarget, Time.deltaTime * _zoomSpeed);

            var viewportPosition = _camera.WorldToViewportPoint(Target.transform.position);

            if (viewportPosition.x <= Borders.Left)
            {
                _snappedTargetPosition = Target.transform.position;
                _isUsingSide           = true;
                _side        = Mathf.Clamp01(_side);
                _targetSide  = 1;
                _sideTimeout = 0;
            }
            else if (viewportPosition.x > 1.0f - Borders.Right)
            {
                _snappedTargetPosition = Target.transform.position;
                _isUsingSide           = true;
                _side        = -Mathf.Clamp01(-_side);
                _targetSide  = -1;
                _sideTimeout = 0;
            }

            if (viewportPosition.y < Borders.Bottom)
            {
                var point = _camera.ViewportToWorldPoint(new Vector3(viewportPosition.x, Borders.Bottom, viewportPosition.z));
                _offset.y -= Vector3.Dot(transform.up, point - Target.transform.position);
            }
            else if (viewportPosition.y > 1.0f - Borders.Top)
            {
                var point = _camera.ViewportToWorldPoint(new Vector3(viewportPosition.x, 1.0f - Borders.Top, viewportPosition.z));
                _offset.y -= Vector3.Dot(transform.up, point - Target.transform.position);
            }

            if (!_isUsingSide)
            {
                _side = Mathf.Lerp(_side, 0, Time.deltaTime * 4);
            }
            else
            {
                _side = Mathf.Lerp(_side, _targetSide, Time.deltaTime * ShiftSpeed);
            }

            if (_isUsingSide)
            {
                const float margin = 0.1f;
                float       shift  = Vector3.Dot(Target.transform.position - _snappedTargetPosition, transform.right);

                if (_targetSide < 0)
                {
                    if (shift > margin)
                    {
                        _snappedTargetPosition = Target.transform.position;
                        _sideTimeout           = 0;
                    }
                    else if (shift < -margin)
                    {
                        _snappedTargetPosition = Target.transform.position;
                        _sideTimeout           = 0;
                        _targetSide            = 1;
                    }
                    else
                    {
                        _sideTimeout += Time.deltaTime;
                    }
                }
                else
                {
                    if (shift < -margin)
                    {
                        _snappedTargetPosition = Target.transform.position;
                        _sideTimeout           = 0;
                    }
                    else if (shift > margin)
                    {
                        _snappedTargetPosition = Target.transform.position;
                        _sideTimeout           = 0;
                        _targetSide            = -1;
                    }
                    else
                    {
                        _sideTimeout += Time.deltaTime;
                    }
                }

                if (_sideTimeout > 0.5f)
                {
                    _isUsingSide = false;
                }
            }
            else
            {
                _sideTimeout = 0;
            }

            const float padding = 0.05f;

            if (_targetSide < 0)
            {
                var point = _camera.ViewportToWorldPoint(new Vector3(Borders.Left + padding, viewportPosition.y, viewportPosition.z));
                _offset.x -= Vector3.Dot(transform.right, point - Target.transform.position) * Mathf.Clamp01(-_side);
            }
            else
            {
                var point = _camera.ViewportToWorldPoint(new Vector3(1.0f - Borders.Right - padding, viewportPosition.y, viewportPosition.z));
                _offset.x -= Vector3.Dot(transform.right, point - Target.transform.position) * Mathf.Clamp01(_side);
            }

            var offset = new Vector3(_offset.x, _offset.y, -Zoom * Distance);

            offset = _orientation * offset;

            var targetOffset = new Vector3(_offset.x, _offset.y, 0);

            targetOffset = _orientation * targetOffset + TargetOffset;

            transform.position = _pivot + offset;
            transform.LookAt(_pivot + targetOffset);

            _pivot  = Target.transform.position;
            offset  = transform.position - _pivot;
            offset  = Quaternion.Inverse(_orientation) * offset;
            _offset = new Vector2(offset.x, offset.y);
        }