Пример #1
0
        public override void Update()
        {
            Vector2 lv        = Vector2.zero;
            Vector2 follow    = Vector2.zero;
            Vector2 evade     = Vector2.zero;
            Vector2 separate  = Vector2.zero;
            Vector2 aligement = _direction;

            if (_leader != null)
            {
                lv  = -_leader._Velocity;
                lv  = lv.normalized;
                lv *= _LEADER_BEHIND_DIST;

                _behind._position = _leader._Position + lv;
                _behind._velocity = _leader._Velocity;
                //_arrive._target = _behind;
                _arrive.Update();
                follow = _arrive._steering;

                _ahead._position = _leader._Position - lv;
                _ahead._velocity = _leader._Velocity;
                float ld2 = (_host._Position - _leader._Position).sqrMagnitude;
                float ad2 = (_host._Position - _ahead._position).sqrMagnitude;
                float r2  = _LEADER_SIGHT_RADIUS * _LEADER_SIGHT_RADIUS;
                if (ld2 < r2 || ad2 < r2)
                {
                    //_ahead._position = _leader._Position - lv;
                    //_ahead._velocity = _leader._Velocity;
                    //_evade._target = _ahead;
                    _evade.Update();
                    evade = _evade._steering;
                }
            }

            //_separation._boids = _flockers;
            _separation.Update();
            separate  = _separation._steering;
            separate  = separate.normalized;
            separate *= _separation._WEIGHT;

            //_aligement._boids = _flockers;
            _aligement.Update();
            aligement = _aligement._direction.normalized;

            _steering  = follow + evade + separate;
            _direction = aligement;
        }
Пример #2
0
        public override void Update()
        {
            Vector2 lv        = Vector2.zero;
            Vector2 follow    = Vector2.zero;
            Vector2 evade     = Vector2.zero;
            Vector2 separate  = Vector2.zero;
            Vector2 aligement = _direction;

            if (_target != null)
            {
                _arrive._target = _target;
                _arrive.Update();
                follow    = _arrive._steering;
                aligement = _arrive._direction;
            }

            if (_flockers != null && _flockers.Count > 0)
            {
                // Evade fire line
                _friend = _flockers[0];
                float fd2 = (_host._Position - _friend._Position).sqrMagnitude;

                foreach (IBoid boid in _flockers)
                {
                    float l = (_host._Position - boid._Position).sqrMagnitude;
                    if (l < fd2)
                    {
                        fd2     = l;
                        _friend = boid;
                    }
                }

                lv  = -_friend._Velocity;
                lv  = lv.normalized;
                lv *= _BEHIND_DIST;
                _ahead._position = _friend._Position + lv;
                _ahead._velocity = _friend._Velocity;
                float ad2 = (_host._Position - _ahead._position).sqrMagnitude;
                float r2  = _SIGHT_RADIUS * _SIGHT_RADIUS;
                if (fd2 < r2 || ad2 < r2)
                {
                    _evade._target = _ahead;
                    _evade.Update();
                    evade = _evade._steering;

                    _ahead._position *= -1;
                    _evade._target    = _ahead;
                    _evade.Update();
                    evade += _evade._steering;
                }

                // Separate from flockers
                //_separation._boids = _flockers;
                _separation.Update();
                separate  = _separation._steering;
                separate  = separate.normalized;
                separate *= _separation._WEIGHT;
            }

            _steering  = follow + evade + separate;
            _direction = aligement;
        }