Spawnable AI Controller Script to chase FPS Controller that is tagged "Player". ( C# )
Hello, I am trying to modify an AIController Script that makes an NPC "Enemy" chase after the FPSController GameObject that is tagged "Player". Ultimately, I would like to spawn the "Enemies" equipped with the AIController Script with an EmenyManger Script that I harvested from the Survival Shooter Tutorial.
I made a prefab of my "Enemy" NPC with an AIController Script that I got from How to make enemy chase player. Basic AI. Instead of having to manually assign Player in the Inspector, I want the script to FindGameObjectsWithTag("Player");
Maybe this is not the best way to go about not having to manually assign the Player - I am open to other suggestions. I am new to coding && having a bunch of trouble figuring this out. Please help! Below is the AIController Script..
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AIController : MonoBehaviour
{
public Transform Player;
int MoveSpeed = 4;
int MaxDist = 10;
int MinDist = 5
void Start()
{
}
void Update()
{
transform.LookAt(Player);
if (Vector3.Distance(transform.position, Player.position) >= MinDist)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
if (Vector3.Distance(transform.position, Player.position) <= MaxDist)
{
//Here Call any function U want Like Shoot at here or something
}
}
}
}