I have tried everything I know of. I even set the player to start off across the map, however the AI still shoots, despite the player needing to be within the radius of the AI. The ai just keeps shooting until the game lags up. What could be the problem?
Why doesn’t my AI shoot at the player? Why does the AI shoot when the player is not near? I am really confused.
var attackInterval = 3;
var bullet : Rigidbody;
var bulletSpeed = 20;
var ammoCount = 100;
var ammoCost = 1;
var minDist = 15;
var hasAmmo : boolean = false;
var withinRange : boolean = false;
var APCollider : Collider;
var spaceshipTransform : Transform;
function Update () {
if(ammoCount < 0) {
ammoCount = 0; // negative ammo isn’t possible, also needed for checkAmmo()
}
checkAmmo();
checkRange();
if(withinRange) {
Attack();
ammoCount -= ammoCost;
}
}
function Attack() {
while (hasAmmo) {
Shoot();
yield WaitForSeconds (attackInterval);
}
}
function checkAmmo() {
if(ammoCount > 0) {
hasAmmo = true;
}
else if(ammoCount == 0) {
hasAmmo = false;
}
}
function checkRange() {
if (spaceshipTransform) {
var dist = Vector3.Distance(spaceshipTransform.position, transform.position);
}
if (dist < minDist) {
withinRange = true;
}
else {
withinRange = false;
}
}
function Shoot() {
var instantiatedProjectile : Rigidbody = Instantiate(bullet, transform.position, transform.rotation );
instantiatedProjectile.velocity = transform.TransformDirection( Vector3( 0, 0, bulletSpeed ) );
Physics.IgnoreCollision( instantiatedProjectile.collider, transform.root.collider );
Physics.IgnoreCollision( instantiatedProjectile.collider, APCollider );
}