- Home /
A* pathfinding generating new path when created new obstacle
Hi, I have small (hope so) problem with generating new path for spawning moobs if I'm creating obstacles in my game. The piece of code is here:
using UnityEngine;
using System.Collections;
public class CreateObstacle : MonoBehaviour {
Ray ray;
RaycastHit hit;
public GameObject yourObstacle;
private float yVariableCorrection = 1f;
int currentObstacles = 0;
private int PossiblePlace;
EnemyAI[] enemies;
void Awake() {
enemies = Component.FindObjectsOfType<EnemyAI>() as EnemyAI[];
}
void Update () {
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
PossiblePlace = LayerMask.NameToLayer ("PossiblePlace");
if (Input.GetMouseButtonDown (0)) {
if (Physics.Raycast (ray, out hit)) {
if (hit.transform.gameObject.layer == PossiblePlace && GameManager.currentMoney >= GameManager.createdObstacle) {
Instantiate (yourObstacle, new Vector3 (hit.point.x, (hit.point.y + yVariableCorrection), hit.point.z), Quaternion.identity);
GameManager.currentMoney -= GameManager.createdObstacle;
Debug.Log("Money: " + GameManager.currentMoney);
currentObstacles++;
AstarPath.active.Scan ();
foreach (EnemyAI enemy in enemies) {
enemy.GetNewPath();
}
}
else {
Debug.Log ("Wrong place or no money");
}
}
}
}
}
The problem is: if I create obstacle, graph is changing and exclude impossible points from graph <- it works great. But when I want regenerate new path for enemies it now work so well. If enemy is actually spawned his path is not changing and can go through my obstacle, Fresh spawned enemies don't have this problem.
Screen:
Your answer
Follow this Question
Related Questions
How do i get the IsPathPossible() function to ignore some nodes using Astar Pathfinding Project 0 Answers
How to create a Drop Down menu for a script file? 1 Answer
How to make the tower shoot at the one who is closer to the finish line? 0 Answers
Script sets same value to other script in all objects instead of just one. 1 Answer
invalid asset Id if player is connected 0 Answers