- Home /
AI Shooting through walls.
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.
Answer by Vitor_r · Jul 29, 2014 at 02:42 PM
If you wall has a collider, you can raycast towards the player, and if the raycast hits the player it means that the AI can see the player and can shoot.
If the raycast hit the wall, the player isn't visible.
It's shooting a lot of bullets because you are doing a raycast every update and setting canShoot=true; A easy solution would be use another variable for the delay.
And test it before the raycast: #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 $$anonymous$$Shoot : float = 2;
var maxShoot : float = 4;
var rayLength = 10000;
var shootWait;
function Start ()
{
e();
canShoot = false;
shootWait=false;
}
function Update ()
{
transform.LookAt(player);
if(!shootWait){
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)
{
shootWait=true;
Shoot();
}
}
}
function e()
{
canShoot=false;
yield WaitForSeconds(Random.Range(1, 2));
canShoot=true;
shootWait=false;
}
function w()
{
canShoot=false;
yield WaitForSeconds(Random.Range($$anonymous$$Shoot,maxShoot));
canShoot=true;
shootWait=false;
}
function Shoot ()
{
var rocketInstance : Transform;
rocketInstance = Instantiate(rocketPrefab, barrelEnd.position, barrelEnd.rotation);
w();
audio.PlayOneShot(soundEffect);
}
I changed it in notepad. $$anonymous$$aybe i wrote something wrong. And i can't check it right now because i dont have Unity here, i'll check it later when i get home :)
But you can try figure out why it's not working, i just changed the "Shoot a lot" code and implemented a way to control when the Ai can or cannot shoot.
Great,thanks a lot! Im still trying to get it to work but my lack of sleep and headache are makign it impossible for me.
Your answer
Follow this Question
Related Questions
Functions being ignored 2 Answers
AI Evasion help 1 Answer
Animation not being played! 0 Answers
Shotgun Spread Issue 1 Answer