Ejemplo n.º 1
0
    private void FixedUpdate()
    {
        if (Target == null)
        {
            return;
        }

        if (Vector3.Distance(transform.position, Target.position) < StoppingDistance)
        {
            // face the target
            Vector3 direction = (Target.position - transform.position).normalized;
            direction.y = 0; // dont look upwards if the target is jumping or above the enemy
            Quaternion rotation = Quaternion.LookRotation(direction);
            transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.fixedDeltaTime);

            if (!isPaused)
            {
                ReachedTarget?.Invoke(transform.position, Resume);
                isPaused = true;
            }
        }
        else
        {
            enemyNavMeshAgent.destination = Target.position;
        }
    }
Ejemplo n.º 2
0
        private IEnumerator Co_MovingToTarget(Vector3 target)
        {
            _navMeshAgent.SetDestination(target);
            yield return(new WaitUntil(() => !_navMeshAgent.pathPending && _navMeshAgent.remainingDistance < 0.1f));

            _state.SetManual();
            ReachedTarget.Invoke();
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Initialization on startup.
 /// Gets the follower component and subscribes on the finish movement event.
 /// </summary>
 private void Awake()
 {
     follower = gameObject.GetComponent <Follower>();
     follower.ReachedTarget += () =>
     {
         ReachedTarget?.Invoke();
     };
     sourceObject = gameObject;
 }
Ejemplo n.º 4
0
        private void ResetTransforms()
        {
            this.gameObject.SetActive(false);

            // this will reset physical position of collider
            _rigidbody.gameObject.transform.position = this.transform.position;
            _rigidbody.velocity = Vector3.zero;

            ReachedTarget?.Invoke();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Starts a movement between the given positions directly.
        /// </summary>
        public IEnumerator MoveTo(Vector3 startPosition, Vector3 targetPosition)
        {
            fraction = 0;
            //Compute the direction vector.
            var direction = targetPosition - startPosition;

            while (fraction < 1) //while not all the path is passed
            {
                //Compute the part of path that is passed and move the object.
                fraction += velocity / direction.magnitude * Time.deltaTime;
                gameObject.GetComponent <RectTransform>().localPosition =
                    Vector3.Lerp(startPosition, targetPosition, fraction);
                yield return(null);
            }
            //The movement is finished.
            ReachedTarget?.Invoke();
        }
    private void FixedUpdate()
    {
        if (isPaused || Target == null)
        {
            return;
        }

        if (Vector3.Distance(transform.position, Target.position) < StoppingDistance)
        {
            ReachedTarget?.Invoke(transform.position, Resume);
            isPaused = true;
        }
        else
        {
            enemyNavMeshAgent.destination = Target.position;
        }
    }
Ejemplo n.º 7
0
        protected void CalcTargetAngle(GameTime gameTime)
        {
            float targetAngle = (float)Math.Atan2((TargetPosition - Position).Y, (TargetPosition - Position).X);
            float difference  = MathHelper.WrapAngle(targetAngle - Angle);

            CalcNewAngle(gameTime, difference);


            if (ReachedTarget != null && Vector2.DistanceSquared(Position, TargetPosition) < targetReachedMarginSquared)
            {
                var methods = ReachedTarget.GetInvocationList();
                ReachedTarget = null;
                foreach (var elem in methods)
                {
                    elem.DynamicInvoke(this);
                }
            }
        }