- Home /
finishing an animation before starting another?
I use this script to make my gun fire and while it's firing, the gun can also play the reload animation and while it's reloading the gun will also be able to shoot. I wanted to create a way of checking if the gun has finished reloading or firing? is there any way of doing this? I googled a bit to try and find something but nothing really matched what I wanted to do. also, yes my script is probably messy I'm new to programming.
using UnityEngine;
using System.Collections;
public class WeaponFire : MonoBehaviour
{
public bool outofammo;
public int bullets = 7;
public GameObject deagle; //this is my gun
public float time = 1;
public float firerate = 1;
public bool fired = false;
public bool Canfire = true;
public bool timedown;
Transform Effect;
public float TakeDamage = 100;
// object particleClone;
RaycastHit hit;
public float Distance;
public float MaxDistance = 2;
public int Damage = 25;
public bool reloading = false;
public GameObject slider;
public bool reloadinggun = false;
void Start()
{
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.R) && bullets < 7) //if R is pressed then this happens
{
deagle.animation.Play ("Reload"); //deagle is my gun, this allows me to play my reload animation if my bullets are lower than 7.
bullets = 7; //after reloading my bullets go back to being 7
reloading = false;
}
if (Input.GetButtonDown("Fire1") && reloadinggun == false) //if I press left mouse button
{
deagle.animation.Play ("Fire");
timedown = true;
RaycastHit hit;
if (time == 1 && reloading == false)
{
if(Physics.Raycast (transform.position, transform.TransformDirection(Vector3.down), out hit))
{
Distance = hit.distance;
if (Distance < MaxDistance)
hit.transform.SendMessage ("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
Debug.DrawLine(transform.position, hit.point, Color.red);
}
}
//timer starts to count down for my firerate
//particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
//Destroy(ParticleClone.gameObject, 2);
if (bullets == 7)
{
reloading = false;
}
if (bullets == 0) //if I run out of bullets, my gun is now out of ammo and the timer will stop otherwise it restarts and skips the first bullet in the gun.
{
reloading = false;
bullets = 7; //after reloading my bullets go back to being 7
outofammo = true; //if my bullets equal 0 then I am out of ammo triggering the next "if" statement
timedown = false; //timedown stops otherwise my gun will fire the first bullet automatically.
}
else
outofammo = false; //if bullets does not equal 0 then I am not out of ammo
if (outofammo == true)
{
reloading = true;
deagle.animation.Play ("Reload"); //if the gun has 0 bullets it will automatically reload
}
}
if (timedown == true) //if my firerate is set to true do this
{
time = (time - (firerate *Time.deltaTime)); //this takes away time from my timer
if (time <= 0) //once the timer is 0 I will be able to fire my gun again
{
bullets = bullets -1; //when my gun shoots a bullet is taken away
fired = true; //after I have fired play the next if statement below
{
timedown = false; //after the time is 0 then it will not continue to go down
time = 1f;
fired = false;
}
}
if (fired == true) //after shooting my time goes back to being 1 so it can count down again
time = 1;
}
}
}
Answer by supernat · Mar 25, 2014 at 06:16 AM
If you open the animation curve editor for your reload animation, when you right click on the timeline, you can place 1 of 2 items: a keyframe, and an event trigger. Place an event trigger at the beginning and one at the end, and specify a method name to call for each. Create the two methods, one to handle the start of the reload and one to process the end. At the start of reload, you should set a flag indicating the reload is happening, and at the end, you can reset the flag. Or you can just use the end event to do something else, depends on your needs.
it seems like I can only add an event but only with "(no function selected)" as shown in the picture attached
Right, you need to create a script with a function (you can make a public void dowhatever()) and attach that script to your game object that has the animation on it. Then it will show up in the pull down here. I believe you may be able to pass a single argument, check the unity documentation if that's something you need to do. Good luck.