- Home /
Is there a way to keep a function from being called until the end of another?
In my game, I have reloading and animations playing, but when I shoot, it messes up the animation and appears in an awkward position/angle. Is there a way to stop the Shooting function (and some others like it) until the Reload function/animation is done?
function Update()
{
if(Input.GetButtonDown("Fire1") && BulletsLeft)
{
if(ShootTimer == 0)
{
Shoot();
ShootTimer = ShootCooler;
}
}
if(Input.GetKeyDown(KeyCode.R))
{
Reload();
}
if(ShootTimer > 0)
{
ShootTimer -= Time.deltaTime;
}
if(ShootTimer < 0)
{
ShootTimer = 0;
}
}
function Shoot()
{
GameObject.Find("Assault_Rifle").animation.Play("Shoot");
PlayShootAudio();
var Hit : RaycastHit;
var DirectionRay = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position, DirectionRay * Range, Color.blue);
if(Physics.Raycast(transform.position, DirectionRay, Hit, Range))
{
if(Hit.rigidbody)
{
Hit.rigidbody.AddForceAtPosition(DirectionRay * Force, Hit.point);
Hit.collider.SendMessageUpwards("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
}
}
BulletsLeft --;
if(BulletsLeft < 0)
{
BulletsLeft = 0;
}
if(BulletsLeft == 0)
{
Reload();
}
}
function Reload()
{
GameObject.Find("Assault_Rifle").animation.Play("Reload");
PlayReloadAudio();
yield WaitForSeconds(ReloadTime);
if(Clips > 0)
{
BulletsLeft = BulletsPerClip;
}
Clips --;
}
Answer by Undead · Jul 28, 2012 at 07:49 AM
First off I would replace the GameObject.Find("Assault_Rifle") with a reference var to the object for performance reasons you don't want to keep using GameObject.Find, you can either add a var at the top of your script :
var assult_rifle : Assult_Rifle;
and drag and drop the object in the inspecter, then you can access it without using the costly find all the time
assult_rifle.animation.Play("Shoot");
Or add a var at the top as above and add a find in your start or awake function to do the lookup so if you forget to assign the object it can still find it..
function Start()
{
assult_rifle = GameObject.Find("Assault_Rifle");
}
As for stopping the Shoot function the easiest way to do this for reloading and others as you mentioned is to add a boolean to the reload (or other funtion you wish to stop for the moment) so you can add an if statment to the function / functions you want to wait.
so I would add something like :
private var canShoot : boolean = true;
at the top with your vars, you can leave it a non private var if you want to access it from other scripts ofc, then change the update where you call the shoot function :
if(Input.GetButtonDown("Fire1") && BulletsLeft)
{
if(ShootTimer == 0)
{
if(canShoot == true)
{
Shoot();
ShootTimer = ShootCooler;
}
}
}
and the add the boolean to your reload and other functions you wish to stop the player from shooting :
function Reload()
{
GameObject.Find("Assault_Rifle").animation.Play("Reload");
canShoot = false; // this will stop the shoot function from being called
PlayReloadAudio();
yield WaitForSeconds(ReloadTime);
canShoot = true; //allows the shoot function to be called again
if(Clips > 0)
{
BulletsLeft = BulletsPerClip;
}
Clips --;
}
Answer by Griffo · Jul 28, 2012 at 06:08 AM
You could try this, replace "GameObject.Find("Assault_Rifle").animation.Play("Shoot");"
if (!animation.IsPlaying(GameObject.Find("Assault_Rifle").animation.Play("Reload"))){
GameObject.Find("Assault_Rifle").animation.Play("Shoot");
PlayShootAudio();
}