- Home /
Destroy Object during animation
So right now I have a weapon with a swinging animation. What I need is for the objects to be destroyed as the weapon collided into it. This worked well, but right now I'm trying to find a way for it to be destroy as the weapon collides into it AND while the animation is playing. I found "Animation.IsPlaying" which seemed helpful but I'm not entirely sure how to implement it. Below is the code that destroys the object as it is being collided into.
public GameObject objtodestroy;
public GameObject effect;
public AudioClip boom;
public Animator anim;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Weapons" && (!anim.IsPlaying("hello")))
{
AudioSource.PlayClipAtPoint(boom, new Vector3(81.5f, 7.7f, 254.8f));
Instantiate(effect, objtodestroy.transform.position, objtodestroy.transform.rotation);
Destroy(this.gameObject);
}
}
}
Answer by UnityNoob123 · Apr 20, 2018 at 08:24 PM
IsPlaying is for an animation, not an animator like what you have in your script. Personally I would use a raycast from the player's camera when some type of input is pressed. Then depending on your animation, do a Ienumerator function that calls some DestroyObject function that is attached to the object you want to destroy. This way you will not need to check if an animation is playing, BUT, that's just me. To work with your code, I think by having a bool variable that is set to true when the animation is playing and then set to false when the animation is done playing. So for the script that handles swinging the weapon try something like this:
public bool isSwinging = false;
Ienumerator Swing() {
isSwinging = true;
yield return new WaitForSeconds(animation time); //After this duration isSwinging is set to false
isSwinging = false;
}
And for the object destroy script try something like this:
void OnTriggerEnter(Collider other) {
var weaponHandler = other.transform.root.GetComponent<name of script for weapon>();
if (other.gameObject.tag == "Weapons" && weaponHandler.isSwinging) {
AudioSource.PlayClipAtPoint(boom, new Vector3(81.5f, 7.7f, 254.8f));
Instantiate(effect, objtodestroy.transform.position, objtodestroy.transform.rotation);
Destroy(this.gameObject);
}
}
Thank you very much for the reply! The second code doesn't have any issues, but I might not have inputted the first code correctly.
https://snag.gy/u$$anonymous$$XmiL.jpg
Here is a screenshot of what my script currently looks like. This is the script that calls the weapon to animate when "C" is pressed. $$anonymous$$aybe I misunderstood and was supposed to change "Swing" to something else?
Right so, it should be IEnumerator, not Ienumerator, my bad. Second, inside the if statement for input, you have to call the Swing function, it does not get called automatically like the start and update methods do. Third, there is a special way to call IEnumerator functions, in this case it would be: StartCoroutine(Swing()); Lastly, in: yield return new WaitForSeconds(animation time); animation time was just a place holder for you, it should be the duration of the animation as a float variable, or you can hard code the float.
For example: yield return new WaitForSeconds(5f);
This means that whatever is underneath this line of code, it will be delayed by 5 seconds. Hope this helps.
The code seems to be fixed right now, but I still can't get the weapon to destroy the object while animating. I'll send you more screenshots of both codes and anything else that might help. I may have something not entirely right either in the code or unity. https://snag.gy/zGwVRy.jpg https://snag.gy/wgBjab.jpg
Answer by premium123 · Apr 25, 2018 at 02:48 AM
Animator.GetCurrentAnimatorStateInfo('layerIndex').IsName("'NameOfState'")
'layerIndex' is the index in the "Animator"-view. (if there is only one layer it is 0) 'NameOfState' is the name of the state in the "Animator"-view :P
this should solve you problem in pseudocode:
if(Animator.GetCurrentAnimatorStateInfo(0).IsName("IsWeaponSwinging"))
DoSomething();
for advanced cases you should read a little bit about 'StateMachineBehaviors' they are really usefull for more difficult constructs :P
Thankyouverymuch for responding, but I managed to get it to work with the other user.
Your answer
![](https://koobas.hobune.stream/wayback/20220612161207im_/https://answers.unity.com/themes/thub/images/avi.jpg)