- Home /
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!
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.
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.)
Which Objekt play the animation the $$anonymous$$ey or the Player?
@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.
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
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.
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)
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.
I have my animation exactly named "$$anonymous$$eyPickedUp", I've tried to test my animation but the GUI's animation isn't working!
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?
Your answer
![](https://koobas.hobune.stream/wayback/20220613105038im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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