Open Sesame

Forums:

Open/Close Doors * After giving it some thought during lunch today, I decided that I should clarify that this is not the "only" way to create animations. This is one way to do it using Unity. There are other methods such as creating animations and importing it with external applications such as Blender. These external options work similarly but the animations are already tied to your models and rigging on import and will show up within your object. External methods like Blender also allow you to use .bvh mocab files that provide complete animation sequences but that process is outside of the scope of this topic. I also did a follow up topic if you are having problems with your controller animation firing here. Fundamentally this should be a simple one. First off, if one does not already exist you must create an animator/animation to do the swing. You can do this by right clicking in assets and select "Create/Animation". Name it something clever and then open the new animation. At this point I believe it will also create both the animation and a new "animation controller" for you. You should use a controller which contains your animation (or multiple). If you just add the animation directly to your object you will get an error message in unity console stating that the object is using a legacy animation. Double click your new animation and it should open up the animation window, which will be empty. It will probably say that it does not have a reference on the bottom right. Don't fret, you can simply drag your "door" or other object from the left hierarchy panel into that display panel on the right. This is where you can watch your animation play. It took me a little while to figure this question out: "how do you get something into the animation builder?". Simply click on the object you want to animate over in the left hierarchy menu (or within your scene you can click on the object that you want to create your animation from) After clicking on the object in either the scene or the hierarchy left panel you will see the option to add "properties". Ahh.. there's the meat and potatoes. If there is already an animation associated it will load it. If there is not, click "create" to create a new one. If it already created one, simply overwrite the new one. The Animation Timeline If you look at the image below, you can see the start on the left and end on the right with two property transitions. Position and Rotation. Basically what you are going to do is create a timeline with a start and end stamp, performing some type of action on the required property (i.e. location,rotation,scale). Depending on your object and where it has its origin located you will need to add a timeline for rotation and position along the required axis. You can either change these manually or by using the "record" button on the player you can click on the time at the top, make your changes using the position or rotation tools. You can also cut and paste the little diamonds allowing you to manipulate the entire timeline. For a door swing, it is basically at 00:00 closed or rotation Y=0. Open is a rotation on the Y axis of something greater than zero.. in my example it is rotation.y=150. Once you have your start and end completed you can click the play button and see the animation. (or at any time really) At this point, you have a new animation which will rotate the door, and position accordingly if required and that animation has been added to your new animation controller. If it is not automatically added to your door object then you will need to add it. Next comes the script to control it and fire the door open anim in the controller. In my case, I will be using a card swipe to control opening the door. This will start a timer and then in X seconds it will close the door. This will consist of the swipe object and a box collider that the card must hit before opening. It will also validate that the card that collides has access to opent the door. This will be controlled via an integer value stored on the card itself. I can use the same script for both the door swipe and the door itself by adding the integer property public int cardID=0; to the script. For the door I will set that to zero and only on the cards themself set the integeter. The door/swipe validation will contain an int[] list of cards to validate against. On the card, this value will be an emply list. On the door, it will contain valid card ID ints. ugh.. like I said.. should be simple right? I guess if you don't want to use validation then you can just open the door for any collision with the box collider and you are done. This would be much simpler. The flow is simple: 1. ID Card (script) with only Card ID integer set. 2. Card Swipe (script) to detect collision and includes validation ID list and door animation game object. 3. On collision, loops through valid integers and if collision object Card ID int is in the list, fires animation and audio, else fires failed audio. 4. Door toggle fires and timer starts. (open if close) 5. Timer expires and door toggle fires. (close if open) Originally I thought door would need script but not so. The swipe will control all operations and audio and the card/badge only contains its ID number.