Пример #1
0
        /// <summary>
        /// Sets up the navigation agent to move to the givent position.
        /// </summary>
        private void updatePath()
        {
            AIUtil.Path(ref _path, transform.position, _target);

            _pathLength       = _path.GetCornersNonAlloc(_pathPoints);
            _currentPathIndex = 0;

            if (_pathLength > _pathPoints.Length)
            {
                _pathLength = _pathPoints.Length;
            }

            if (_pathLength > 1)
            {
                var vector   = _pathPoints[1] - _pathPoints[0];
                var distance = vector.magnitude;

                if (distance > 0.3f)
                {
                    updateDirection(vector / distance, true);
                }
            }

            _hasCheckedIfReachable      = false;
            _positionToCheckIfReachable = _target;
        }
Пример #2
0
        /// <summary>
        /// Sets up the navigation agent to move to the givent position.
        /// </summary>
        private void updatePath()
        {
            AIUtil.Path(ref _path, transform.position, _target);

            _pathLength       = _path.GetCornersNonAlloc(_pathPoints);
            _currentPathIndex = 0;

            if (_pathLength > 1)
            {
                updateDirection((_pathPoints[1] - _pathPoints[0]).normalized, true);
            }
        }
Пример #3
0
        /// <summary>
        /// Told by the brains to investigate a position.
        /// </summary>
        /// <param name="position"></param>
        public void ToInvestigatePosition(Vector3 position)
        {
            _isInvestigating = true;

            _position = position;
            _cover    = null;
            var minDistance = 0f;

            for (int i = 0; i < Physics.OverlapSphereNonAlloc(position, CoverSearchDistance, _colliders, 0x1 << 8, QueryTriggerInteraction.Collide); i++)
            {
                var cover = CoverSearch.GetCover(_colliders[i].gameObject);

                if (cover != null)
                {
                    var point    = cover.ClosestPointTo(position, 0.3f, 0.3f);
                    var distance = Vector3.Distance(position, point);

                    if (distance < minDistance || _cover == null)
                    {
                        _cover      = cover;
                        _position   = point;
                        minDistance = distance;
                    }
                }
            }

            _verifyDistance = Util.GetViewDistance(_position, VerifyDistance, true);

            if (_cover == null)
            {
                _hasReachedCoverLine = false;

                if (isActiveAndEnabled)
                {
                    Message("ToWalkTo", position);
                    Message("OnInvestigationStart");
                }
            }
            else
            {
                var vector = _position - transform.position;
                _hasReachedCoverLine = Vector3.Dot(_cover.Forward, vector) > 0;

                if (_hasReachedCoverLine)
                {
                    if (isActiveAndEnabled)
                    {
                        Message("ToWalkTo", _position);
                        Message("OnInvestigationStart");
                    }
                }
                else
                {
                    var left  = _cover.LeftCorner(_cover.Bottom, CoverOffset) - _cover.Forward * 1.0f;
                    var right = _cover.RightCorner(_cover.Bottom, CoverOffset) - _cover.Forward * 1.0f;

                    AIUtil.Path(ref _path, transform.position, left);
                    var leftLength = 0f;

                    if (_path.status == NavMeshPathStatus.PathInvalid)
                    {
                        leftLength = 999999f;
                    }
                    else
                    {
                        for (int i = 1; i < _path.GetCornersNonAlloc(_corners); i++)
                        {
                            leftLength += Vector3.Distance(_corners[i], _corners[i - 1]);
                        }
                    }

                    AIUtil.Path(ref _path, transform.position, right);
                    var rightLength = 0f;

                    if (_path.status == NavMeshPathStatus.PathInvalid)
                    {
                        rightLength = 999999f;
                    }
                    else
                    {
                        for (int i = 1; i < _path.GetCornersNonAlloc(_corners); i++)
                        {
                            rightLength += Vector3.Distance(_corners[i], _corners[i - 1]);
                        }
                    }

                    if (leftLength < rightLength)
                    {
                        _approachPosition = left;
                    }
                    else
                    {
                        _approachPosition = right;
                    }

                    var distance = Vector3.Distance(_approachPosition, _position);

                    if (distance + VerifyRadius > _verifyDistance)
                    {
                        _approachPosition = _position + Vector3.Normalize(_approachPosition - _position) * (_verifyDistance + VerifyRadius - 0.1f);
                    }

                    if (isActiveAndEnabled)
                    {
                        Message("ToWalkTo", _approachPosition);
                        Message("OnInvestigationStart");
                    }
                }
            }
        }