Patrol System for instatianted Game Objects,How do I make a patrol system but the instanted objects have their unique patrol
My code currently doesn't work for instatianted objects for some reason as its a prefab but if it's not a prefab it does work. I need a way for the prefab to work and to check if their is already an object going towards that move spot and if their is find a new move spot. I really need help on this been stuc on this for about a week. If you have an answer please explain with the code and make sure it works in 2D, thanks!
{ public float speed; public Transform moveSpot; private float waitTime; public float startWaitTime; public float minX; public float maxX; public float minY; public float maxY; void Start() { waitTime = startWaitTime; moveSpot.position = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
} // Uses the random number to move to random location void Update() { transform.position = Vector2.MoveTowards(transform.position, moveSpot.position, speed * Time.deltaTime); // If it's near the random location it set to acheive destination if(Vector2.Distance(transform.position, moveSpot.position) < 0.2f) { if(waitTime <= 0) { moveSpot.position = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY)); waitTime = startWaitTime; }else{ waitTime -= Time.deltaTime; } } }
}
So I need a way to detect if an instanted object is already following the "moveSpot" and if it is to find another available "moveSpot" and for it to work for prefabs as well. ( This is for a 2D game ).
Your answer
Follow this Question
Related Questions
A* 2D Pathfinding not working with prefabs? 0 Answers
2d Pathfinding in Unity 5.6, how it works? 0 Answers
File Path Confusion for Sharing an Image 0 Answers
2D platformer AI 0 Answers
Multiple patrolling enemies 0 Answers