Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jiomancy · Apr 20, 2018 at 06:30 PM · animationobjectdestroy

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);
         
     }
    
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
     }
 }


Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jiomancy · Apr 21, 2018 at 01:37 AM 0
Share

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?

avatar image UnityNoob123 jiomancy · Apr 21, 2018 at 02:04 AM 0
Share

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.

avatar image jiomancy UnityNoob123 · Apr 21, 2018 at 03:30 AM 0
Share

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

Show more comments
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jiomancy · Apr 26, 2018 at 05:01 PM 0
Share

Thankyouverymuch for responding, but I managed to get it to work with the other user.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

217 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

destroying object after animation 3 Answers

Object gone aftar play - Animation problem. 0 Answers

Animation messes up if GameObject is destroyed 1 Answer

how do i destroy a spawned object 1 Answer

Obj calls function, on another obj and get's destroyed. Function stops 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges