Exemplo n.º 1
0
 public void Stop()
 {
     if (IsNavRunning())
     {
         NavAgent.SetDestination(base.transform.position);
     }
 }
Exemplo n.º 2
0
 /**
  * This method moves the enemy towards the player
  */
 public void ChasePlayer()
 {
     //Health check
     if (health > 0)
     {
         NavAgent.SetDestination(Target.position);
     }
 }
Exemplo n.º 3
0
 /**
  * This method moves the enemy towards a random position inside the search radius
  */
 public void IdleMove()
 {
     //Health check
     if (health > 0)
     {
         NavAgent.SetDestination(RandomNavmeshLocation(idleSearchRadius));
         Timer = 0;
     }
 }
Exemplo n.º 4
0
    // Update is called once per frame
    void Update()
    {
        // Distance to the target
        float distance = Vector3.Distance(target.position, transform.position);

        // If inside the lookRadius
        if (distance <= lookRadius)
        {
            if (!isBennAttack)
            {
                // Move towards the target
                agent.SetDestination(target.position);

                // If within attacking distance
                if (distance <= agent.stoppingDistance)
                {
                    CharacterStats targetStats = target.GetComponent <CharacterStats>();
                    if (targetStats != null)
                    {
                        combat.Attack(targetStats);
                        agent.collider.anim.SetBool("attack", true);
                    }
                    else
                    {
                        agent.collider.anim.SetBool("attack", false);
                    }
                }
            }
            else
            {
                agent.collider.velocity = Vector3.zero;
            }
            FaceTarget();   // Make sure to face towards the target
        }
        else
        {
            agent.SetDestination(agent.transform.position);
        }

        if (stats.currentHealth <= 0)
        {
            StartCoroutine(Die());
        }
    }
Exemplo n.º 5
0
 public void Walk(Vector3 direction)
 {
     if (NavAgent == null)
     {
         Vector3 translate = transform.position + direction * walkSpeed * Time.deltaTime;
         TryMove(translate, collisionMask);
         return;
     }
     NavAgent.SetDestination(transform.position + direction * walkSpeed);
 }
Exemplo n.º 6
0
    // Update is called once per frame
    void Update()
    {
        RaycastHit2D blocked = Physics2D.Linecast(transform.position, target.position, 1 << 8);

        if (!blocked)
        {
            if (nav.CheckPath(target.position))
            {
                nav.SetDestination(target.position);
            }
        }
    }
Exemplo n.º 7
0
 public override void SetDestination(Vector3 newDestination)
 {
     if (IsNavRunning())
     {
         if (AiManager.setdestination_navmesh_failsafe && !NavAgent.isOnNavMesh)
         {
             PlaceOnNavMesh();
         }
         base.SetDestination(newDestination);
         if (NavAgent.enabled)
         {
             NavAgent.SetDestination(newDestination);
         }
     }
 }
Exemplo n.º 8
0
 private void GoToNextPoint()
 {
     if (pathQueue.Count > 0)
     {
         Ap -= 1;
         Vector3 aim = pathQueue.Pop();
         NavAgent.SetDestination(aim);
         NavMeshPath path = new NavMeshPath();
         NavAgent.CalculatePath(aim, path);
         Controller.Walk(true);
     }
     else
     {
         NavAgent.ResetPath();
         Controller.Walk(false);
     }
 }
        protected override void FSMUpdate()
        {
            HasPath     = NavAgent.hasPath;
            PathPending = NavAgent.pathPending;
            PathStale   = NavAgent.isPathStale;
            PathStatus  = NavAgent.pathStatus;

            if (target != null)
            {
                NavAgent.SetDestination(target.position);
            }

            if ((NavAgent.remainingDistance > NavAgent.stoppingDistance && !PathPending) ||
                PathStatus == NavMeshPathStatus.PathInvalid)
            {
                SetNextDestination(true);
            }
            else if (NavAgent.isPathStale)
            {
                SetNextDestination(false);
            }
        }
Exemplo n.º 10
0
    // Update is called once per frame
    void Update()
    {
        if (isMoving)
        {
            // TODO: If a unit is blocked, but is "near enough" its destination, allow it to stop there
            // (i.e., for formation movement). Need to somehow consider whether a unit moved as part of the
            // same order has reached its destination already.
            if (unitIsBlocked && hasNextPosition)
            {
                Vector2Int nextPos2Int = MapController.Instance.WorldToCell(nextPosition);
                if (MapController.Instance.UpdateUnitPosition(this, nextPos2Int))
                {
                    unitIsBlocked = false;
                }
            }

            if (!hasNextPosition && navAgent.HasNextCell())
            {
                Vector2Int nextPos2Int = navAgent.PopNextCell();
                nextPosition = MapController.Instance.GetCellCenterWorld(nextPos2Int);

                hasNextPosition = true;
                // Immediately update the map position to the neighbouring square
                if (!MapController.Instance.UpdateUnitPosition(this, nextPos2Int) && !unitIsBlocked)
                {
                    unitIsBlocked = true;
                    // If the agent is blocked and the destination cell is not occupied,
                    // calculate a new path.
                    if (!MapController.Instance.mapModel.IsOccupied(navAgent.destination))
                    {
                        navAgent.RecalculatePath();
                        hasNextPosition = false;
                    }
                    else if (onBlockedAction != null)
                    {
                        hasNextPosition = false;
                        navAgent.SetDestination(onBlockedAction.Invoke());
                    }
                }
            }
            else if (!hasNextPosition && !navAgent.HasNextCell())
            {
                isMoving      = false;
                unitIsBlocked = false;
            }

            if (hasNextPosition && !unitIsBlocked)
            {
                transform.position = Vector2.MoveTowards(transform.position, nextPosition, Time.deltaTime * moveSpeed);

                if (Vector2.Distance(transform.position, nextPosition) == 0f)
                {
                    hasNextPosition = false;
                    if (!navAgent.HasNextCell() && onArriveAction != null)
                    {
                        // When unit arrives at destination, invoke onArriveAction
                        onArriveAction.Invoke();
                    }
                }
            }
        }

        if (InputController.Instance.isSelecting && InputController.Instance.IsInSelectRect(this))
        {
            // TODO: Visual indicator of tentative selection?
        }
    }
Exemplo n.º 11
0
 public void MoveTo(Vector3 nPos)
 {
     SetMovePos(nPos);
     NavAgent.SetDestination(MovePos);
 }