Hello There!
Before you read more about my problem, keep in mind I don’t have that much coding experiance, so forgive me if the answer is obivious.
My code basically makes my AI shooter stay put until the player is within a certain distance. If the player goes within that certain distance, the enemy will follow the player until it gets to a range to shoot. The problem with my script is that my AI starts shooting my player immedietly after play mode is active. The bool that tells me that the player has been seen by the enemy isn’t even checked!
I dont want my enemyAttack() function to start immedietly, so how do I make it not start immidetly?
Here’s my script:
public class EnemyAI2 : MonoBehaviour
{
public Transform M_Player;
public float startTimeBtwShots;
private float timeBtwShots;
public GameObject projectiles;
public float bulletSpeed = 5f;
public GameObject shootPosition;
public float enemySpeed = 10f;
public bool playerSeen = false;
public Transform Player;
public int MoveSpeed = 4;
public int MaxDist = 10;
public int MinDist = 5;
public int seeDist = 60;
// Use this for initialization
void Start()
{
playerSeen = false;
}
// Update is called once per frame
void Update()
{
if (Vector3.Distance(transform.position, Player.position) <= seeDist)
{
playerSeen = true;
}
if (Vector3.Distance(transform.position, Player.position) > seeDist)
{
playerSeen = false;
}
if (playerSeen = true)
{
enemyAttack();
}
if (playerSeen = false)
{
patrol();
}
}
public void enemyAttack()
{
transform.LookAt(Player);
GetComponent<NavMeshAgent>().destination = M_Player.position;
GetComponent<NavMeshAgent>().speed = enemySpeed;
if (Vector3.Distance(transform.position, Player.position) <= MinDist)
{
if (timeBtwShots <= 0)
{
Instantiate(projectiles, transform.position, transform.rotation);
projectiles.transform.position = shootPosition.transform.position = transform.forward;
projectiles.transform.forward = shootPosition.transform.forward;
timeBtwShots = startTimeBtwShots;
}
else
{
timeBtwShots -= Time.deltaTime;
}
}
if (Vector3.Distance(transform.position, Player.position) > MaxDist)
{
gameObject.GetComponent<NavMeshAgent>().isStopped = false;
}
if (Vector3.Distance(transform.position, Player.position) <= MaxDist)
{
gameObject.GetComponent<NavMeshAgent>().isStopped = true;
}
}
public void patrol()
{
gameObject.GetComponent<NavMeshAgent>().isStopped = true;
}
}
Thank you!
Have a goood day!