Bryan Bedard's Blog
My adventures in software development, mistakes and all.
< Previous  1  2  3  Next >

Modifying Animations for Prefabs in Unity

Posted by Bryan Bedard - 12/27/2017

It was not obvious to me how to go about adding an animation to an existing prefab or modify animations associated with an existing prefab. This forum discussion led me to the right answer but I wanted to write a short blog post so I can easily find my way back to this answer in the future when my memory of it becomes foggy.

You can’t directly add animations to a prefab nor modify animations it already has. The only way to do it is to follow these steps:

  1. Create an instance of your Prefab. (e.g. drag it onto the scene)
  2. Add/modify animation clips on the instance.
  3. When finished, drag the instance on top of the original Prefab to replace it with the enhanced object that includes the new or updated animations.

Relative Position Animation in Unity

Posted by Bryan Bedard - 12/27/2017

Unity has a powerful animation editor. The concept is that you attach an Animator component to a GameObject and then attach an Animator Controller to the Animator. An Animator Controller allows you to arrange and maintain a set of Animation Clips and associated Animation Transitions for a character or object. An Animation Clip lets you define animations to GameObject properties along a timeline of keyframes.

I ran into a snag when I tried to animate the Transform.Position property of a GameObject. This works well for an instance of a GameObject in a specific location. However, a problem arises if you try to re-use the animation as part of a prefab or if you move the GameObject to another position. The problem is that the position values stored in the animation clip are absolute, meaning the object will move to the position recorded in the animation (world coordinates), instead of applying the animation relative to the current position of the GameObject prior to starting the animation (local coordinates).

This video tutorial by Erwin Broekhuis explains the simple fix for this, which is to wrap the animated object in a parent object and attach the Animator to the parent instead. However, the video is using the Animation component which has been deprecated in favor of the newer Animator component. It took me a couple attempts to get it working properly so I decided to write this blog post with updated screen shots and steps with Animator.

Step 1: Create the Parent Object

Create an empty object off the root of the hierarchy. You can name the parent whatever you like, here it is called Parent.

Screen shot of Create Parent

Step 2: Add the Child Object

Add the child object under the parent. In this walkthrough, we are simply adding a Cube 3D object. The child object should default to transform.position (0, 0, 0), which is a local coordinate position relative to the parent. You can name the child whatever you like, here it is called Cube.

Screen shot of Create Child

Step 3: Add the Animator Component to the Parent

Add an Animator component to the parent object. The Animator component is found in the miscellaneous category.

Screen shot of Create Animator

Step 4: Create an Animator Controller

We need to create an Animator Controller to manage the animation clips and state transitions. I suggest creating a folder named Animation to store your animation controller and animation clips. In the Project window, right-click on the Animation folder and choose Create > Animator Controller. You can name the controller whatever you like. Here it is called CubeAnimatorController.

Screen shot of Create Animator Controller

Step 5: Attach the Animator Controller to Animator

Attach the Animator Controller to the Animator by dragging it onto the Controller property of the Animator. To do this, start by selecting the Parent object in the hierarchy then click on the Animation folder in the Project window. Drag CubeAnimatorController from the Animation folder onto the empty Controller box under Animator in the Inspector window.

Screen shot of Attach Animator Controller

Step 6: Create an Animation Clip

If it’s not open already, open the Animation window by choosing Window > Animation from the Unity Editor menu. You can place the Animation window anywhere you like. Personally, I like to position the Animation window in the same group as my Project window in such a way that the Scene window is visible at the same time as the Animation window.

Select the Cube object in the Hierarchy window. Click on the Animation tab so that the Animation window is visible. You should be prompted to create an animation clip.

Screen shot of Create Animator Clip

When you press the button to create an animation clip a dialog will open prompting for the location to save the animation clip. I suggest saving it in your Animation folder where you saved the Animator Controller. Name the animation Clip CubeMove.

Screen shot of Animation Clip Properties

Note that creating the animation clip also adds a corresponding state for it to the CubeAnimatorController you attached to the Animator. This blog post does not cover anything related to the Animator Controller or state transitions.

Step 7: Add the Cube’s Transform.Position Property to the Animation Clip

In the Animation window, with the CubeMove clip (state) selected, click the Add Property button. Do not select the root Transform object but instead expland Cube and choose Cube > Tranform > Position.

At this point, you will see the Cube Position property in the Animation window with two keyframes added to the timeline, one at 0:00 and one at 1:00. These times refer to the frame number. By default an animation has 60 samples and will last 1 second at 60 frames per second.

Screen shot of Cube Transform Position Property

Step 8: Add a Keyframe to the Animation

Click on the timeline at position 0:30 then click the button in the Animation window to add a keyframe. You will notice that the Animation editor will enter “record” mode with a few visual cues:

  • The record button will be depressed
  • The game Play/Pause buttons at the top of the Unity Editor will be colored red
  • The Transform.Position property attributes in the Inspector for the Cube will be colored red

Change one of the Transform.Position property attributes. For example, change X from 0 to 2.

Note: At this point you may see that both the Parent and the Cube objects are selected in the Hierarchy window and that a Transform.Position will be added to the animation clip for both of them. If this happens, right click the Parent Transform.Position and choose to remove the properties from the animation. This is key to ensuring that the animation remains relative to the parent object’s current position.

Screen shot of Add Keyframe to Animation

Click the Record button in the Animation window to stop recording.

Step 9: Test the Animation

Press the Play button in the Animation window to test your animation. You should see your Cube sliding 2 units along the X axis from its start position then return back.

Step 10: Move the Parent Object and Repeat Test

To confirm that the relative position animation works as expected, move the Parent object somewhere else in the scene then click the Play button in the Animation window again. If everything is configured correctly, the animation should begin from the new location of Parent and animate 2 units along the X axis and back. It should not jump to the location where the animation was originally recorded.

The Music of the Night on Piano - Blue Yeti

Posted by Bryan Bedard - 8/29/2017

I bought a Blue Yeti USB microphone this weekend and I absolutely love it. I got the Blackout version and it looks very sexy :)

Blue Yeti Microphone

To test out the new mic, I re-recorded The Music of The Night on piano. It sounds SO much better than my original recording. I used Cyberlink PowerDirector and AudioDirector to edit the video and cleanup the audio, but the mic is so good I really didn't have to do much to the audtio at all. Eliminated a bit of room noise (really wasn't much) and bumped the volume up a tad.

All I Ask of You on Piano

Posted by Bryan Bedard - 7/10/2017

Me playing All I Ask of You from Phantom of the Opera on piano.

Happy 10th Anniversary TerraClues!

Posted by Bryan Bedard - 2/22/2017

In celebration of 10 years since the launch of TerraClues, we are excited to re-launch the site with a modern look and feel. The best part of the new design is that it is mobile friendly and works great on touch devices of any size. Help us celebrate by grabbing your tablet or phone and curl up on the couch and enjoy solving a few hunts.

TerraClues home page

In addition to the new look and feel, there are a couple new features. Easily share hunts with friends on Facebook and Twitter. Check out the new Stats page to understand how far people are making it into your hunt before getting stuck.

TerraClues features

It’s now easier than ever to find interesting hunts to play. Click tags in hunt listings to quickly see other hunts with the same tag. Tags are available for categories, difficulty levels and more. Be sure to tag your hunts when create new ones or circle back and add tags now to hunts you created before.

Hunts tagged in hunt lists

TerraClues has always been popular in schools and teachers around the world use it as a fun and educational game in their classroom. To make it easier for students to jump in and play, we simplified the Schools area. Hunts have been carefully selected by our team of moderators and tagged as educational and appropriate for use within a classroom setting. Additionally, hunts are tagged for specific grade levels or subjects. Browse available hunts using the tags on the Schools page Tap into students' creativity by challenging them to create their own hunts too. A free account is required to create hunts.

TerraClues for Schools

We realize the Schools area is dramatically different than before. The original version had powerful features like the ability to create private classrooms, curate specific hunts for them to play and track their progress. Don’t panic, we didn’t take these features away completely. You can still access the full Classic Mode of the schools area.

We would love your feedback on the new design, good or bad. Email us at or reach out to us on Facebook or follow us on Twitter.

< Previous  1  2  3  Next >