private void Update() { if (parentPopulation) { //take care of the removal of individual when time run out timeLeft -= Time.deltaTime; if (timeLeft <= 0) { parentPopulation.IndividualExhausted(); Destroy(gameObject); } switch (status) { //search of a food node or individual if population is agressive case Status.searching: if (Vector3.Distance(target.position, transform.position) < 0.6f) { target = generatePosition(); aiDestinationSetter.target = target; aIPath.SearchPath(); } break; //if a target is found, go to it case Status.seeking: if (aiDestinationSetter.target && Vector3.Distance(aiDestinationSetter.target.position, transform.position) < 1.2f) { status = Status.eating; } break; //eat the food or individual, increasing the food amount for the population case Status.eating: if (aiDestinationSetter.target) { aIPath.maxSpeed = 0; if (aiDestinationSetter.target.tag == "Food") { parentPopulation.foodEaten(Time.deltaTime / 2 + (stats[(int)GameInfo.PopulationStat.stat.bite] / 10)); } else { parentPopulation.foodEaten(Time.deltaTime * (stats[(int)GameInfo.PopulationStat.stat.agression] / 20)); } } break; } //if the food has decayed, go back to finding a new target if (!aiDestinationSetter.target) { status = Status.searching; target = generatePosition(); aiDestinationSetter.target = target; aIPath.maxSpeed = stats[(int)GameInfo.PopulationStat.stat.speed] * 2f + 15f; aIPath.SearchPath(); } } }
private void Chase() { if (!_chasing) { _chasing = true; _AIPath.maxSpeed = chaseSpeed; _AIPath.target = _player; _AIPath.SearchPath(); } }
public void MoveTo(Vector3 destination) { ai.destination = destination; if (ChangedDestination != null) { ChangedDestination.Invoke(); } ai.SearchPath(); StartCoroutine(WaitForPathComplete()); }
//MovementTarget movementTarget; void Update() { if (Input.GetMouseButton(1) && !playerAI.canMove && !waitForRender) { if (EventSystem.current.IsPointerOverGameObject()) { return; } RaycastHit hit = RaycastGround(); if (hit.transform.tag == "Ground") { waitForRender = true; pointer.gameObject.SetActive(false); playerAI.destination = hit.point; playerAI.SearchPath(); // Дальше, как только ИИ вычислит путь, сработает 'pathCallback', и запустится подписанный на него // метод 'RenderPath', который отрисует путь. } if (hit.transform.tag == "Movement Target") { waitForRender = true; pointer.gameObject.SetActive(false); MovementTarget movementTarget = hit.transform.GetComponent <MovementTarget>(); if (movementTarget.hasAlternativeDestination) { playerAI.destination = movementTarget.alternativeDestination.position; playerAI.SearchPath(); } else { playerAI.destination = hit.transform.position; playerAI.SearchPath(); } // Дальше, как только ИИ вычислит путь, сработает 'pathCallback', и запустится подписанный на него // метод 'RenderPath', который отрисует путь. } } if (!Input.GetMouseButton(1)) { pointer.gameObject.SetActive(true); } }
void startMoveToTarget() { print(name + " startMoveToTarget"); if (target.GetComponent <UnitScript>().isDead()) { stopMoveToTarget(); activateUnit = false; } aIDestinationSetter.target = target.transform; aIPath.SearchPath(); isMoving = true; aIPath.canSearch = true; aIPath.canMove = true; return; }
public void MoveToPosition(Vector3 targetPosition) { path.isStopped = false; path.destination = targetPosition; // !!! path.SearchPath(); }
/// <summary> /// Resets interact target and navigates to position /// </summary> /// <param name="pos">Where the object will try to move</param> #region Movement public void MoveTo(Vector3 pos) { m_InteractTarget = null; m_PathLocator.position = pos; m_Path.target = m_PathLocator; m_Path.SearchPath(); }
void Dinamico() { if (patrolCoroutine != null) { return; } if (patrolMovements.Length < 1) { return; } aIDestinationSetter.target = patrolMovements[patrolIndex].transform; aiPath.SearchPath(); aiPath.maxSpeed = speed; patrolCoroutine = StartCoroutine(CheckReachedPatrolPoint()); }
// Use this for initialization void Start() { _AIPath = GetComponent <AIPath>(); _player = PlayerMovement.instance.transform; _chasing = true; _AIPath.maxSpeed = chaseSpeed; AstarPath.active.logPathResults = PathLog.OnlyErrors; _AIPath.target = _player; _AIPath.SearchPath(); }
void StartMoving() { if (!HasPath) { HasPath = true; mov.canMove = true; mov.canSearch = true; mov.target = player.transform; mov.SearchPath(); } }
void UpdateMovePosition(Vector3 p) { move_p = new Vector3(p.x, transform.position.y, p.z); moving = true; AIPathFinder.canSearch = true; //AIPathFinder.canMove=true; AIPathFinder.SetVectorTarget(move_p); AIPathFinder.SearchPath(); ResetStuckSys(); }
public void WaypointWander(float timeBetweenMovements, Transform[] waypoints) { if (currentWaypoints.Count == 0) { foreach (Transform waypoint in waypoints) { currentWaypoints.Add(waypoint.position); } } if (isWaiting) { elapsedTime += Time.deltaTime; if (elapsedTime > timeBetweenMovements) { elapsedTime = 0; isWaiting = false; } } else { if (ai.reachedEndOfPath) { isWaiting = true; currentWaypointIndex++; if (currentWaypointIndex >= currentWaypoints.Count) { currentWaypointIndex = 0; } } if (!ai.pathPending && (ai.reachedEndOfPath || !ai.hasPath)) { ai.destination = currentWaypoints[currentWaypointIndex]; ai.SearchPath(); } } }
public void Think() { if (m_action.target == null) { m_action.distance = Vector3.Distance(m_controller.transform.position, m_action.interactionPosition); if (m_action.status != UnitAction.ActionStatus.Planned && m_navigation.reachedEndOfPath && !m_navigation.pathPending) { m_action.status = UnitAction.ActionStatus.Ended; } else { m_action.status = UnitAction.ActionStatus.Moving; } } else //if (m_action.target) { if (m_controller.InteractionWith(m_action.target) == UnitAction.ActionType.None) { m_action.target = null; m_action.status = UnitAction.ActionStatus.Cancelled; m_action.cancellable = true; } else { if (!m_action.target.isStatic) { UpdateAction(ref m_action, true); } m_action.distance = World.Distance(m_controller.unit, m_action.target); if ((m_action.distance < m_action.actionMinRange || m_action.distance > m_action.actionMaxRange) && !m_navigation.pathPending) { m_navigation.isStopped = false; m_action.status = UnitAction.ActionStatus.Moving; } else if (m_action.distance < m_action.actionMaxRange) { m_navigation.isStopped = true; m_action.status = UnitAction.ActionStatus.Working; } } } if (m_action.status == UnitAction.ActionStatus.Moving && m_action.interactionPosition != m_navigation.destination) { m_navigation.destination = m_action.interactionPosition; m_navigation.isStopped = false; m_navigation.SearchPath(); } }
void SetDestination() { // Maybe turn this into a coroutine so we can pause at the start if we're at a bank or distraction _destinationIndex++; RadialNode nextDestination = _path.GetNode(_destinationIndex); if (nextDestination == null) { OnExitReached?.Invoke(); return; } Vector3 randomPoint = nextDestination.RadialPosition; _ai.destination = randomPoint; _ai.SearchPath(); }
private static int SearchPath(IntPtr L) { int result; try { ToLua.CheckArgsCount(L, 1); AIPath aIPath = (AIPath)ToLua.CheckObject(L, 1, typeof(AIPath)); aIPath.SearchPath(); result = 0; } catch (Exception e) { result = LuaDLL.toluaL_exception(L, e, null); } return(result); }
void OnLevelWasLoaded(int level) { Net_SyncData(); SceneManager.SceneTransition transition = SceneManager.instance.NewestTransition; if (null == transition) { return; } // Find list of locators GameObject[] locators = GameObject.FindGameObjectsWithTag("Locator"); if (locators.Length == 0) { Debug.LogWarning("No locator in scene"); return; } // Find a locator of this name GameObject locator = null; foreach (GameObject locObj in locators) { if (locObj.name == transition.LocatorName) { locator = locObj; } } if (null == locator) { Debug.LogWarning("No Locator of name " + transition.LocatorName); return; } transform.position = locator.transform.position; transform.rotation = locator.transform.rotation; // Reset our path follower m_Path.target = transform; m_Path.SearchPath(); }
public void Init(PopulationController parentPopulation, GameInfo.PopulationStat populationStat) { this.parentPopulation = parentPopulation; this.stats = populationStat.stats; SphereCollider agroCollider = GetComponentInChildren <SphereCollider>(); aiDestinationSetter = GetComponent <AIDestinationSetter>(); aIPath = GetComponent <AIPath>(); //set the base stats of the individual from its population stats aIPath.maxSpeed = stats[(int)GameInfo.PopulationStat.stat.speed] * 2f + 15f; agroCollider.radius = stats[(int)GameInfo.PopulationStat.stat.vision] * 2 + 10; timeLeft += stats[(int)GameInfo.PopulationStat.stat.endurance] * 1.5f; newTarget = new GameObject().transform; target = generatePosition(); aiDestinationSetter.target = target; aIPath.SearchPath(); }
IEnumerator FillPathLengthsDictionary() { for (int i = 0; i < bins.Length; i++) { binsSearchingAI.destination = bins[i].transform.position; binsSearchingAI.SearchPath(); yield return(new WaitWhile(() => pathToBin == null)); float pathLength = 0; Vector3 lastCorner = (Vector3)pathToBin.path[0].position; foreach (GraphNode currentCorner in pathToBin.path) { pathLength += Vector3.Distance(lastCorner, (Vector3)currentCorner.position); lastCorner = (Vector3)currentCorner.position; } pathLengthByBin.Add(bins[i], pathLength); pathToBin = null; } fillPathLegthsDictionary_CR_running = false; }
public void SetTarget(Transform target) { this.target = target.gameObject; aiPath.SearchPath(); }
// navigate to the currentTarget, if you take too long, you're probably stuck, teleport out. once you get there, do the task we're here to do. IEnumerator GoToTarget(Target target) { if (currentTarget != null) { int timeout = 10; while (target.chickens.Count >= target.maxSpots && currentTarget != null && timeout >= 0) // while this place is full, wait until it's not full { timeout--; yield return(new WaitForSeconds(1 / gameManager.gameSpeed)); } if (timeout > 0 && currentTarget != null) { if (target != null) { target.chickens.Add(chicken); // reserve a space } if (target.parentTarget != null) { Target parentTargetInfo = target.parentTarget.GetComponent <Target>(); parentTargetInfo.chickens.Add(chicken); } ai.destination = currentTarget.transform.position; ai.SearchPath(); float timeStarted = gameManager.time; while (currentTarget != null && ((transform.position - currentTarget.transform.position).sqrMagnitude) > 1f) // while we're not at the currentTarget yet, adn the currentTarget does still exist { if (gameManager.time - timeStarted > 500) // if it's been 500 'time' (minutes), then lets' teleport there // this number will change later idk what it should be { if (currentTarget != null) { transform.position = currentTarget.transform.position; Debug.Log("I was stuck and teleported"); } else // if the currentTarget doesn't exist, it blew up or something, start from scratch and get a whole new job { jobTaskComplete = true; RemoveReservation(); Debug.Log("ay the currentTarget exploded while i was on the way?"); } } yield return(new WaitForSeconds(1 / gameManager.gameSpeed)); } if (resourceNeeded != Resource.None) // if we're trying to get { //Debug.Log(chicken.name + " is going to " + action.ToString() + " " + resourceNeeded.ToString() + " at " + currentTarget.name); } else { //Debug.Log(chicken.name + " is going to " + action.ToString()); } if (currentTarget != null) { DoAction(target); } else { Debug.Log("ay the currentTarget exploded while i was on the way?"); jobTaskComplete = true; RemoveReservation(); } } else { Debug.Log("Target had no empty slots"); jobTaskComplete = true; RemoveReservation(); } } else { Debug.Log("There was no Target to navigate to"); jobTaskComplete = true; RemoveReservation(); } }
public override void Update() { base.Update(); if (refineryHomebase == null) { refineryHomebase = FindOreRefinery(); if (refineryHomebase == null) { return; } } switch (mineState) { case MineState.IDLE: //check if both minerals and a ore factory exists, iff -> SEARCH float shortestDistance = Vector3.Distance(transform.position, resources[0].transform.position); closestResource = resources[0]; for (int i = 1; i < resources.Length; i++) { float distance = Vector3.Distance(transform.position, resources[i].transform.position); if (distance < shortestDistance) { shortestDistance = distance; closestResource = resources[i]; } } ai.destination = closestResource.transform.position; ai.SearchPath(); mineState = MineState.SEARCH; break; case MineState.SEARCH: if ((ai.reachedEndOfPath && !ai.pathPending) || ai.remainingDistance < 6) { mineState = MineState.MINE; mineTime = Time.time; } break; case MineState.MINE: if (mineTime + mineInterval < Time.time) { addOreToInventory(closestResource.getResourceValue()); mineTime = Time.time; } if (inventorySize <= currentInventory) { if (refineryHomebase) { ai.destination = refineryHomebase.GetEntrancePosition(); ai.SearchPath(); mineState = MineState.RETURN; } } break; case MineState.RETURN: if ((ai.reachedEndOfPath && !ai.pathPending) && refineryHomebase) { enterUnloadState(); Vector3 _direction = (refineryHomebase.transform.position - transform.position).normalized; Quaternion _lookRotation = Quaternion.LookRotation(_direction); transform.rotation = Quaternion.Slerp(transform.rotation, _lookRotation, 0.35f); transform.position = Vector3.MoveTowards(transform.position, refineryHomebase.GetUnloadPosition(), 0.2f); if (Vector3.Distance(transform.position, refineryHomebase.GetUnloadPosition()) < 1f) { mineState = MineState.UNLOAD; } } break; case MineState.UNLOAD: refineryHomebase.AddResources(inventoryValue); Reset(); mineState = MineState.EXIT; break; case MineState.EXIT: if (refineryHomebase) { transform.position = Vector3.MoveTowards(transform.position, refineryHomebase.GetEntrancePosition(), 0.3f); if (Vector3.Distance(transform.position, refineryHomebase.GetEntrancePosition()) < 2f) { GetComponent <Collider>().enabled = true; //ai.gravity = savedGravity; ai.enabled = true; mineState = MineState.IDLE; } } break; } }
public void SetTarget(Transform target) { aiPath.destination = target.position; aiPath.SearchPath(); }