Triggered Animations in Unity 3d

Sharing is caring:

Using Triggers to Play Animation Clips

In this Unity 3d video we’ll be returning to a basic but very useful way of triggering animations. We’ve covered this topic before but I decided that this needed to be updated with some nice new assets and some sound effects.

So we’ll be using a free asset from the asset store of a animated treasure chest that opens up when we walk into a trigger. When our tagged “player” walks into the trigger the animation will play on our chest and play an audio clip as well. Once both those things have been completed the trigger will be destroyed.

This can be directly used to have a player open up hidden chests throughout your game that contain hidden items and power ups. The script used for this video can be found below, along with the links to the assets and sound clip used.

 

Sound Clip Used

http://soundbible.com/1354-Opening-Casket.html

 

Treasure Chest Model w/Animations

https://www.assetstore.unity3d.com/#/content/8952

 

Javascript File

ChestOpen.js


var chestSound : AudioClip; //chest opening sound clip goes here
var treasureChest : GameObject; //treasure chest prefab goes here

function OnTriggerEnter (col : Collider) {

if(col.gameObject.tag == "Player") { //checks to see that our character controller with tag "Player" has entered the trigger
AudioSource.PlayClipAtPoint(chestSound, transform.position); //plays our soundclip at position of collider/trigger
treasureChest.animation.Play(); //plays the default animation applied to our treasureChest model
Destroy(gameObject);// destroys the gameobject that has this script, so our collider in this case

}
}

 

Jonathan Gonzalez

I love all things in game development and want to contribute by teaching others how to create games. I'm always looking to help others create awesome digital worlds. I've been a hardcore gamer since I was a kid and now it's my turn to create awesome digital experiences.

More Posts - Website

Follow Me:
TwitterYouTube

Comments

  1. This is great!! I’ve rotated the chest but when the animation plays the chest moves back to its original position. What am I doing wrong?

    • Jonathan Gonzalez says:

      Create a prefab of that chest with the animation clip applied to it then add it to your scene. I’ve had that issue before, but I can’t think off the top of my head what the main cause was. Just make sure one of them works, then create a prefab and drag it into your scene.

      • I’ve created a prefab: treasure_boxPREFAB with box_top and Cube as children. When I highlight treasure_boxPREFAB the Inspector shows me Animation-Animation: box_open. I’m dropping the prefab into my scene, then rotating it, but when I walk into the trigger the chest instantly switched back to the original position. Are you able to do this without it switching back?

        • Jonathan Gonzalez says:

          I’ll have to try this out myself. I didn’t really have this issue but I’ll let you know what I find out.

          • Hey Jonathan, any luck with recreating my issue?

          • Jonathan Gonzalez says:

            I just tried this so I know what issue you were having. The quick and easy way to resolve this is to make an empty game object and use that as the parent of the treasure chest. The animation will now work off the coordinates of the empty game object. It may take a little tweaking to get down but that’s what I figured out. The newer Mecanim system is much better about this kind of stuff, but I just wanted to recreate this older tutorial and it used the legacy style animations.

  2. hmm, did all the shares, and the script never became available

  3. Tnx This is great!! Make more tuts .

  4. thanks man.. its so realy best.. thanks a script,,

  5. Thanks … simple, i have troubles with Visual Studio 2013, but with mono works well. VS dont reconize .js ? Any recomendations with editor configurations ?

  6. Chad M McGinnis says:

    What would this code look like using C# ?

    • This method of playing animations is outdated. I would recommend using the Mecanim system for all animations. In C# it would look something like this:

      Animator anim;

      void OnTriggerEnter(Collider col)
      {
      if(col.CompareTag(“Player”))
      {
      anim.SetBool(“chestOpen”, true);
      //play audio clip and do anything else
      }
      }

      This would be applied to the animated object that has the Animator component (and animator controller assigned).

Speak Your Mind

*