- Home /
Functions being ignored
Hello, I made this script but for some reason some functions are being ignored or something. This is the 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;
var rayLength = 1000;
function Start ()
{
// e();
}
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;
}
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);
audio.PlayOneShot(soundEffect);
w();
}
The problem here is that the ai ignores(I think) the function "w". Some help would be really appreciated. Thank you for your time.
do you have a gameObject tagged with "Player"?
does the Shoot()
function get called?
btw, ins$$anonymous$$d of this:
if(hit.collider.gameObject.tag != "Player")
{
canShoot = false;
}
if(hit.collider.gameObject.tag == "Player")
{
canShoot = true;
}
you can do this:
canShoot = (hit.collider.gameObject.tag == "Player");
EDIT: do you get any error messages?
Yes,I do have a gameObject tagged with "Player" and Shoot() does get called.
Also,thanks for the suggestion,it will save me a lot of time in the future.
Edit :
No,I don't get any error messages and I just noticed that the "w" function is being called but for some reason it ignores the yield WaitForSeconds part.
if Shoot()
gets called, then w()
should be too... since are there no further conditions to be met
what makes you think that "some functions are being ignored or something"?
I don't know if 'ignored' is the right word for it but the ai is supposed to fire once per 2-4 seconds and not 100 bullets per second.
that's because you're resetting the canShoot
variable every frame when the hit occurs.
try something like this:
if(Physics.Raycast(transform.position, forward, hit, rayLength))
{
if((hit.collider.gameObject.tag == "Player") && canShoot)
{
Shoot();
}
}
Answer by Ssiroo · Jul 29, 2014 at 04:20 PM
I was wrong,ignoring wasn't the case.All the functions are being called correctly,it's just that my script is wrongly written ,Im still trying to figure out how to fix it.
Answer by FranciscoFontes · Jul 29, 2014 at 04:17 PM
[Sorry for my english] To make sure that the unity ignoring the function ... Debug.Log("w") After that it is easier to know which way the program is being run
function w()
{
canShoot=false;
yield WaitForSeconds(Random.Range(minShoot,maxShoot));
canShoot=true;
Debug.Log("w");
}
function Shoot()
{
Debug.Log("Shoot");
var rocketInstance : Transform;
rocketInstance = Instantiate(rocketPrefab, barrelEnd.position, barrelEnd.rotation);
audio.PlayOneShot(soundEffect);
w();
}
Your answer
Follow this Question
Related Questions
AI Shooting through walls. 1 Answer
AI Evasion help 1 Answer
Animation not being played! 0 Answers
Shotgun Spread Issue 1 Answer
Make tranform follow raycast 1 Answer