So, I need help making NPCs follow a road to a random destination. I have multiple roads ( all tagged roads) I need help finding a way for them to find and then follow a road and change roads if needed to get to destination. NPCs will spawn at different locations. Also, I can’t use NavMesh, because something about my map won’t allow it so I have to do it manually. Any Help?
Here is my code:
public Animator animator;
public GameObject target;
public GameObject destination;
public float speed = 2f;
Rigidbody rig;
// Start is called before the first frame update
void Start()
{
rig = GetComponent<Rigidbody>();
animator = GetComponent<Animator>();
target = GameObject.FindWithTag("Roads");
}
// Update is called once per frame
private void FixedUpdate()
{
Vector3 pos = Vector3.MoveTowards(transform.position, target.transform.position, speed * Time.fixedDeltaTime);
rig.MovePosition(pos);
}
public void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Roads")
{
RandomDestination();
Vector3 pos = Vector3.MoveTowards(transform.position, destination.transform.position, speed * Time.fixedDeltaTime);
rig.MovePosition(pos);
}
}
public void RandomDestination()
{
int Des = Random.Range(0, 14);
if (Des == 0)
{
destination = GameObject.Find("AionaTailoring");
}
else if (Des == 1)
{
destination = GameObject.Find("Clinic");
}
else if (Des == 2)
You get the point after this.