Good day (first post on these forums
).
I’m working on a very small game (what game doesn’t really matter). My opponents shoot at me when they get into range (that’s what I have this script for):
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 );
}
However, it sprays the bullets all over the place and yield in Attack() doesn’t work. What is wrong?