Hello ,I made a AI shooting script for my new game but the problem is that the AI ignores the walls and shoots through walls. This is my ai script :
#pragma strict
public var rocketPrefab : Transform;
public var barrelEnd : Transform;
var canShoot : boolean = true;
var shootRate : float;
var gun : Transform;
var soundEffect : AudioClip;
var player : Transform;
var enemy : Transform;
var minShoot : float = 2;
var maxShoot : float = 4;
function Start ()
{
e();
canShoot = true;
}
function Update ()
{
transform.LookAt(player);
if(canShoot==true)
{
Shoot();
}
}
function e()
{
canShoot=false;
yield WaitForSeconds(Random.Range(1, 2));
canShoot=true;
}
function w()
{
canShoot=false;
yield WaitForSeconds(Random.Range(minShoot,maxShoot));
canShoot=true;
}
function Shoot ()
{
var rocketInstance : Transform;
rocketInstance = Instantiate(rocketPrefab, barrelEnd.position, barrelEnd.rotation);
w();
audio.PlayOneShot(soundEffect);
}
What Im trying to do is make the AI shoot only when there is nothing between them. I tried adding a box collider(trigger on) and making it shoot only when the player is inside and it worked but while colliding with other objects it made the game really laggy and it wasn’t looking so good. This is the script I made :
#pragma strict
var ai : Ai;
function Start ()
{
ai = transform.parent.GetComponent(Ai);
ai.enabled = false;
}
function OnTriggerStay ()
{
ai.enabled = true;
}
function OnTriggerExit ()
{
ai.enabled = false;
}
I also destroyed the object when the AI died using this script :
var enemy : Transform;
var ai : Ai;
var aiDeath : AIDeath;
var aiDeath1 : AIDeath1;
var head : Transform;
var sounds : AudioClip[];
var nextlevel : NextLevel;
var enemySight : Transform;
function Start()
{
ai = GetComponent(Ai);
aiDeath = GetComponent(AIDeath);
aiDeath1 = head.GetComponent(AIDeath1);
nextlevel = GameObject.FindWithTag("NextLevel").GetComponent(NextLevel);
}
function OnTriggerEnter(col : Collider)
{
if(col.gameObject.tag=="Bullet")
{
enemy.animation.Play("BodyShot");
audio.PlayOneShot(sounds[Random.Range(0, sounds.length)]);
Destroy (enemySight.gameObject);
nextlevel.enemies -= 1;
Destroy (ai);
Destroy (aiDeath);
Destroy (aiDeath1);
Destroy (enemy.animation, 1);
}
}
Some help would be awsome! Thank you for your time.
Update :
@Vitor_r - I guess your answer would look kind of like this :
#pragma strict
public var rocketPrefab : Transform;
public var barrelEnd : Transform;
var canShoot : boolean = true;
var shootRate : float;
var gun : Transform;
var soundEffect : AudioClip;
var player : Transform;
var enemy : Transform;
var minShoot : float = 2;
var maxShoot : float = 4;
var rayLength = 10000;
function Start ()
{
e();
canShoot = false;
}
function Update ()
{
transform.LookAt(player);
var hit : RaycastHit;
var forward = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(transform.position, forward, hit, rayLength))
{
if(hit.collider.gameObject.tag != "Player")
{
canShoot = false;
}
else if(hit.collider.gameObject.tag == "Player")
{
canShoot = true;
}
}
if (canShoot == true)
{
Shoot();
}
}
function e()
{
canShoot=false;
yield WaitForSeconds(Random.Range(1, 2));
canShoot=true;
}
function w()
{
canShoot=false;
yield WaitForSeconds(Random.Range(minShoot,maxShoot));
canShoot=true;
}
function Shoot ()
{
var rocketInstance : Transform;
rocketInstance = Instantiate(rocketPrefab, barrelEnd.position, barrelEnd.rotation);
w();
audio.PlayOneShot(soundEffect);
}
It works,the AI doesn’t shoot through the wall anymore but when it sees the player,it shoots like 100 bullets per second.It seems to be ignoring my “w” function somehow, I think.