- Home /
Unity3D Ai wont shoot
So my AI enemy wont shoot at the player some of the time, some of my enemy prefabs wont shoot at the player they just look at it. I've tried a whole bunch of things but nothing has worked. Please help, if you get this AI working your name will go into my game credits.
My AI script (contains shooting and walking)
var distance;
var target : Transform;
var lookAtDistance = 15.0;
var stopDistance = 3;
var moveSpeed = 5.0;
var damping = 6.0;
var damage:float = 17;
var range = 100.0;
var force = 10.0;
var muzzleFlash : Renderer;
var muzzleLight : Light;
var Health = 100;
var deadReplacement : Transform;
var dieSound : AudioClip;
static var isDead = false;
static var allowfire : boolean = true;
function Start (){
muzzleFlash.enabled = false;
muzzleLight.enabled = false;
allowfire = true;
}
function Update () {
target = GameObject.FindWithTag("Player").transform;
distance = Vector3.Distance(target.position, transform.position);
if((distance < lookAtDistance)){
lookAt ();
}
if(allowfire == true){
fire();
}
}
function lookAt (){
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
animation.Play("walk");
}
function ApplyDammage (TheDammage : int)
{
Health -= TheDammage;
if(Health <= 0)
{
Dead();
}
}
function Dead () {
// Destroy ourselves
Destroy(gameObject);
Destroy(transform.parent.gameObject);
// Play a dying audio clip
if (dieSound)
AudioSource.PlayClipAtPoint(dieSound, transform.position);
}
static function CopyTransformsRecurse (src : Transform, dst : Transform) {
dst.position = src.position;
dst.rotation = src.rotation;
for (var child : Transform in dst) {
// Match the transform with the same name
var curSrc = src.Find(child.name);
if (curSrc)
CopyTransformsRecurse(curSrc, child);
}
}
function fire(){
allowfire = false;
audio.Play();
var direction = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
// Did we hit anything?
if (Physics.Raycast (transform.position, direction, hit, range)) {
Debug.DrawRay(transform.position, direction * range, Color.green);
// Apply a force to the rigidbody we hit
if (hit.rigidbody)
hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
muzzleFlash.renderer.enabled = true;
muzzleLight.enabled = true;
yield WaitForSeconds (0.04);
muzzleFlash.renderer.enabled = false;
muzzleLight.enabled = false;
yield WaitForSeconds(0.3);
allowfire = true;
}
function OnTriggerEnter(hit:Collider){
if(hit.tag == "Player"){
hit.transform.SendMessage("Damage",damage);
}
}
Answer by robertbu · Feb 02, 2014 at 03:28 PM
This problem is on line 21. You've made 'allowfire' static. This mean that all AI enemies share a single value for this variable, so only one AI enemy at a time will be able to fire. Since you call 'fire()' every frame, the first AI called will be the one that fires. Since the AI enemy scripts will be called in some order, the first one called will be the same for each firing resulting other AI enemies not firing at all.
I'm not sure what your intended behavior is here or how many AI enemies you have, so I cannot propose a fix. If you just remove 'static', all of your AI enemies will fire, but firing will overlap.
O$$anonymous$$G thank you, the reason it was static is cause I copied the players shooting script over but made some changes and forgot that. thank you so much