Beispiel #1
0
        // Start is called before the first frame update

        public override IEnumerator MovementRoutine(Transform _transform, Vector2 _origin)
        {
            Transform         player   = GameObject.Find("Player").transform;
            MovementBehaviour movement = _transform.gameObject.GetComponent <MovementBehaviour>();

            Vector2 waypoint = NewWaypoint(_transform, player);

            float   timer    = 0.0f;
            float   duration = (waypoint - (Vector2)_transform.position).magnitude / maxDistance * durationRef;
            Vector2 origin   = _transform.position;


            while (timer < duration)
            {
                _transform.position = Vector2.Lerp(origin, waypoint, celerityCurve.Evaluate(timer / duration));

                yield return(null);

                timer += Time.deltaTime;
            }

            _transform.position = waypoint;
            yield return(null);

            movement.StartCoroutine(Move(movement, _transform, player));
        }
Beispiel #2
0
        private IEnumerator Move(MovementBehaviour _movementBehaviour, Transform _transform, Transform _player)
        {
            bool    canFlee  = true;
            Vector2 waypoint = NewWaypoint(_transform, _player);

            float   timer    = 0.0f;
            float   duration = (waypoint - (Vector2)_transform.position).magnitude / maxDistance * durationRef;
            Vector2 origin   = _transform.position;


            while (timer < duration)
            {
                _transform.position = Vector2.Lerp(origin, waypoint, celerityCurve.Evaluate(timer / duration));

                if (canFlee && Vector2.Distance(_transform.position, _player.position) < maxDistance * 2)
                {
                    _movementBehaviour.StartCoroutine(FleeRoutine());
                    goto jump;
                }

                yield return(null);

                timer += Time.deltaTime;
            }

            _transform.position = waypoint;
            yield return(null);


jump:
            _movementBehaviour.StartCoroutine(Move(_movementBehaviour, _transform, _player));

            IEnumerator FleeRoutine()
            {
                canFlee = false;

                yield return(new WaitForSeconds(0.5f));

                canFlee = true;
            }
        }