Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 crusherxman · Jun 28, 2013 at 12:28 PM · animationscenepickup

Playing animation when picking up an object.

So, I've been working hours by making scenes and editing my project. I've also managed to get a "key script" in order to proceed next scenes but there's something else that I want to add. As the title says, I want an animation to play once my GameObject (Key) is removed from the scene after picking it up.

If you need more info, here's my key script (Java):

Pick up script:

 var hasKey = false;
  
 function Update(){
 if(Input.GetKeyDown(KeyCode.E)){ //If E is pressed
 Debug.Log("Pressed");
 var hit : RaycastHit;
 if (Physics.Raycast (transform.position, transform.forward, hit, 100)) { // Sends out a raycast to check if something is in front of you
        Debug.Log("hit Something " + hit.transform.name);
 if(hit.transform.name.Equals("Key")){ // If the object in front of you has the tag Key
 hasKey = true;
 Destroy(hit.transform.root.gameObject);//Destroy Key Object
 }else if(hit.transform.name.Equals("Door")){ //Checks if the gameobject you're looking at has the tag Door
 if(hasKey)
 Debug.Log("HasKey");
 hit.transform.SendMessage("Unlock"); //Calls the function Unlock on the door
 }
 }
 }
 }

If you guys have a good way to solve this question, please leave an answer below! I would really appreciate it :)

Thanks!

Comment
Add comment · Show 4
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 bodec · Jun 28, 2013 at 12:37 PM 0
Share

set the animation to play once has$$anonymous$$ey is true and set it to play once. that way has key is true you don't keep playing.. or you may need one more bool call it something like animplay set to false then a if stamen checking haskey is true and animplay is false, play animation set animplay to true so it can't be triggered again.

avatar image TonyLi · Jun 28, 2013 at 01:23 PM 1
Share

What animation system are you using (Legacy or $$anonymous$$ecanim)?

Unity Free or Unity Pro? (Some handy animation features are only available in Pro, but there are workarounds.)

What game object will the animation play on (e.g., the player character, the door, etc.)?

Do you already have the animation clip? (For example, a clip of the character bending down and picking something up.)

Do you need to sync up the animation with anything in the scene? (For example, the character's hand needs to sync up with the position of the key.)

avatar image Oribow · Jun 28, 2013 at 02:13 PM 0
Share

Which Objekt play the animation the $$anonymous$$ey or the Player?

avatar image crusherxman · Jun 30, 2013 at 02:39 PM 0
Share

@TonyLi I'm using a Legacy animation, I'm using Unity Pro, my animation plays on a GUI, I have my animation, and I don't need to sync up with anything else.

2 Replies

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

Answer by crusherxman · Aug 24, 2013 at 02:50 PM

Sorry guys but this answer is really outdated... I've changed to this one: http://answers.unity3d.com/questions/520102/playing-animation-when-gameobject-is-destroyed.html

Comment
Add comment · 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
1

Answer by Stormizin · Jun 28, 2013 at 06:17 PM

Try:

 var hasKey = false;
  
 function Update(){
 if(Input.GetKeyDown(KeyCode.E)){ //If E is pressed
 Debug.Log("Pressed");
 var hit : RaycastHit;
 if (Physics.Raycast (transform.position, transform.forward, hit, 100)) { // Sends out a raycast to check if something is in front of you
     Debug.Log("hit Something " + hit.transform.name);
 if(hit.transform.name.Equals("Key")){ // If the object in front of you has the tag Key
 hasKey = true;
 Destroy(hit.transform.root.gameObject);//Destroy Key Object
 animation.Play("myAnimation");
 }else if(hit.transform.name.Equals("Door")){ //Checks if the gameobject you're looking at has the tag Door
 if(hasKey)
 Debug.Log("HasKey");
 hit.transform.SendMessage("Unlock"); //Calls the function Unlock on the door
 }
 }
 }
 }

Put a Play animation after the key destroyed.

Comment
Add comment · Show 25 · 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 crusherxman · Jun 30, 2013 at 02:31 PM 0
Share

I keep getting this error:

The animation state $$anonymous$$eyPickedUp could not be played because it couldn't be found! Please attach an animation clip with the name '$$anonymous$$eyPickedUp' or call this function only for existing animations. UnityEngine.Animation:Play(String, Play$$anonymous$$ode) UnityEngine.Animation:Play(String) (at C:\BuildAgent\work\7535de4ca26c26ac\Runtime\ExportGenerated\Editor\Animations.cs:562) TestPick up:Update() (at Assets\Standard Assets\Scripts\General Scripts\$$anonymous$$odels\$$anonymous$$ey Function\Door\TestPick up.js:12)

avatar image TonyLi · Jun 30, 2013 at 03:32 PM 0
Share

In the Inspector, check the Animation component. In the list of animation clips, you need to have an animation named exactly "$$anonymous$$eyPickedUp". If it's not there, find the animation in your project and add it to the list.

Before going back to your code, test out the animation by selecting your GUI object and opening the Animation view window. Then select $$anonymous$$eyPickedUp from the drop-down menu and press the play button.

avatar image crusherxman · Jul 01, 2013 at 12:00 AM 0
Share

I have my animation exactly named "$$anonymous$$eyPickedUp", I've tried to test my animation but the GUI's animation isn't working!

avatar image crusherxman · Jul 01, 2013 at 12:00 AM 0
Share

Should I attach my GUI to the Player's camera?

avatar image TonyLi · Jul 01, 2013 at 03:02 AM 0
Share

To keep the scenario as simple as possible until the problem is fixed, I recommend making sure the animation works on the GUI object in the Animation view window first.

Can you post a screenshot of the Inspector view of the GUI's Animation component?

And also a screenshot of the Animation view window with the GUI object selected?

Show more comments

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

19 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

Related Questions

Pickable item to unlock doors (Being able to proceed next scene) 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Animation play when player collides/hits an GameObject 0 Answers

Getting a list of mecanim states in Animator 9 Answers

How can I set the default pose in the scene editor 0 Answers


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