- Home /
Question by
angelmendez003 · Apr 14, 2018 at 06:42 PM ·
c#unity 2dpathfindingavoidance
How to avoid objects while following a random path? unity 2d,How do I avoid objects while following a set of nodes randomly? unity 2d
Hello unity members, I have an enemy object set up to move toward random nodes in a 2d maze environment. The only issue I'm having is keeping the enemy from squeezing through collision walls like a Roach. Here is the code I followed along from a tutorial to move the enemy around with the way points. All help is appreciated, its been almost a week now since I've been trying to wrap my head around this!
public class Enemy_AI : MonoBehaviour {
public float speed;
public float waitTime;
public float startWaitTime;
public Transform[] moveSpots;
private int randomSpot;
private void Start()
{
randomSpot = Random.Range(0, moveSpots.Length);
waitTime = startWaitTime;
}
void Update()
{
transform.position = Vector2.MoveTowards(transform.position, moveSpots[randomSpot].position, speed * Time.deltaTime);
if(Vector2.Distance(transform.position, moveSpots[randomSpot].position) < 0.2f)
{
if(waitTime <= 0)
{
randomSpot = Random.Range(0, moveSpots.Length);
waitTime = startWaitTime;
}
else
{
waitTime -= Time.deltaTime;
}
}
}
}
Comment
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 Can I Access a variably from a coroutine? 1 Answer
Randomly Spawning 4 cherries in Pacman game (Unity) 1 Answer
Set AI Destination Setter target from Script 2 Answers
Multiple Cars not working 1 Answer