Stealth game tutorial – 402 – Enemy Animator Controller – Unity Official Tutorials

Stealth game tutorial – 402 – Enemy Animator Controller – Unity Official Tutorials

In a different way to how we use key input to control our player character’s animator the animator controller for enemies will be based on the velocity of the nav mesh agent. The internal AI in Unity. Let’s being by making this animator controller. Select the Animators folder in the Project panel and click Create – Animator Controller. We will call it Enemy Animator. Then ensure that you have the Animator window visible, go to Window – Animator if you can’t already see it. For the game’s enemies we are going to use four float parameters. Click the plus icon to the right of the parameters field at the bottom of the Animator window and choose Float. The first will be called AngularSpeed. and remember to capitalise as I am doing here. Next we have Speed. Shot and Aim Weight. And finally one Boolean parameter PlayerInSight. AngularSpeed will control how fast the enemy changes it’s facing direction. Speed will be used to control how fast the enemy moves. Shot is a parameter that refers to a curve on the shooting animation. Let’s take a look at these curves briefly. In our shooting animation we can see a curve called Shot. We will use this to determine when the gun is firing and thus when the effects and damage should occur. The curve is set to 0 until the recoil of the shooting animation, at which point the curve peaks to 1 and then returns to 0. We use this value of 1 to define the point at which the effects and damage should occur. You can see hear that this animation has another curve for AimWeight, one of our parameters. When the enemy is shooting the player we want the arm to be pointing at the player so we are going to override the animation using inverse kinematics. This will point the hand of the enemy and therefore his arm at the player. This float curve will define by how much we override the animations with IK. It stays at a constant of 1 for the shooting animation. It goes from 0 to 1 in the raise animation. And from 1 to 0 in the lowering animation. If we did not do this the animation would always show the character simply raising the gun and aiming directly forward even if the player is not directly in front of the enemy. Finally our last parameter, PlayerInSight, will be used to dictate the enemies behaviour when the enemy can see the player and this will be set by the EnemySight script that we will make in the next assignment. Our enemies movement is going to be entirely stored within one 2D blend tree. To start right click in the Animator window and choose Create State – From New Blend Tree. Select the state now and name this Locomotion at the top of the Inspector. And now double click the state to edit the blend tree. We are also going to call this blend tree Locomotion so ensure it’s selected and name it at the top of the Inspector. We want the type of this blend tree to be 2D Freeform Cartesian This is the best type to use for dealing with speed and angular speed. Next we need to choose the parameters that we want to represent the two axis of our 2d blend tree. The X axis will be represented by the angular speed parameter and the Y axis by speed. This means that when we have a map of animations the motions towards the left will be those that turn left. The ones on the right will be turning right. The ones at the top will be running and the ones at the bottom will be walking or stationary. The next step is to add all of the motions that we are going to use. We will start by adding the Turn On The Spot animations. Open the Animations – Humanoid folder and locate humanoid_turnOnSpot and humanoid_idle. Within humanoid_turnOnSpot you will find TurnOnSpotLeftD through to A and TurnOnSpotRightA through to D. And in humanoid_idle you will find the idle animation. That A to D animations represent an width of turning on the spot with A being the smallest and D being the widest turn. Reselect the Locomotion blend tree in order to see the motion list once more. Click the plus icon and select Add Motion Field. Start by dragging on TurnOnSpotLeftD through to A as shown. Creating a new motion field each time. Next create a motion field and apply idle to it. Now create motion fields for TurnOnSpotRightA through to D With 2D blend trees it doesn’t matter which order the animations are applied. But it is nice to have a structure that’s easy to understand. Next we will have the left walks, the straight walk and the right walks. Still in the Humanoid folder, open all of the humanoid_walkForward FPXs in order to find the animations. Begin by adding the left walks, add WalkLeftShort, WalkLeftMedium and WalkLeftWide. Now add the straight forward walk, simply called Walk. Now add the right walks, WalkRightShort, WalkRightMedium and WalkRightWide. Lastly we will add the running animation. These can be found under the humanoid_runForward FPXs. You should note that there are no short runs so we’ll add medium and wide on the left then straight and then wide and medium on the right. Next we need to compute the positions of our animations on the 2D plane.>From the Compute Positions drop down at the bottom select Speed and Angular Speed. This will automatically give our motions thresholds for Speed and Angular Speed based on the root motion that Unity detects. within each animation. Note that calculating the thresholds in this way is possible with any animations regardless of which parameters are present in the controller. This is a calculation build in to the blend tree based on root motion. We have chosen to name two of our parameters Speed and Angular Speed because they coincide with this measurement of root motion, not for functional reasons as this makes it easier to see how they will effect the enemy’s animation. An important thing to note about this way of computing positions is that Angular Speed is calculated in radians rather than degrees. If you are unfamiliar with radians they are simply another measure of an angle. where there are 360 degrees in a full circle there are two pi radians. Thus a full turn is little more than 6 radians, and turning 180 degrees is a little more than 3 radians. You can see the extreme turn on spot animations are around 2.5 and this is almost a 180 degree turn. Now we can preview how this motion will look on our character, let’s make sure that we can see the Preview window and let’s preview using our actual character rather than the default Unity humanoid. So in the Models folder drag and drop char_robotGuard on to the Preview window and press play. To control the preview of a 2D blend tree like this drag the red dot around in the display. This controls the values of Speed and Angular Speed. whilst our motions are represented by the blue dots on the display. And to remind you, Speed is increased further up the display we go, whilst Angular Speed is divided between the left and the right sides. That concludes the movement part of the enemy’s animator controller. Next we need to make a new animation layer for shooting. This layer will mean that the enemy will be able to shoot whilst turning on the spot in the base layer of animation. To start out let’s add a new layer by pressing the plus button. We will call this layer Shooting. The next thing we need to add to our layer is an avatar mask. Create one called EnemyShootingMask within the Animators folder. So select it and choose Create – Avatar Mask. We only need a body mask so expand Body Mask and we want all of the upper body to be controlled by the shooting layer, so simply deselect the legs and the feet. Now we can apply this to the shooting layer. Click the circle icon to the right of Mask and choose EnemyShootingMask. As we mentioned earlier we want the enemy’s arm to point at the player when it is shooting. We’re going to do this by using IK so make sure that you check the IK pass box at the bottom of the layer. Now we can start making states for our shooting layer. We’re going to make our default state an empty state. To do this right click and select Create State – Empty. We need an Empty state so that what ever is happening in the base layer is by default not being overridden. Next we need our RaisedWeapon state. Open the Animations – Humanoid folder and find the animation in the humanoid_weapon_raise FPX. Drag and drop WeaponRaise in to the Animator. We want to make sure that the animation happens nice and quickly so that the enemy can get to shooting right away, so for this reason we’ll change the speed of the animation to 3. We need a transition to RaiseWeapons from our Empty state. The condition for this is that Player In Sight is true. Let’s make sure that we lay out our states with enough space so that they all fit on screen. Then right click on New State and make a transition to WeaponRaise. Select the transition. And make sure that the condition is that Player In Sight is true. Once the weapon has been raised we need him to start shooting, so drag in the ShootWeapon animation. You’ll find this under humanoid_weapon_shoot. Drag and drop it in and align it with our other states. Now we need a transition from the raising of the weapon to the shooting of the weapon and the condition for this should simply be that the animation has finished. So we can leave this on the default transition condition, which is Exit Time So right click on WeaponRaise and make a transition to WeaponShoot. You’ll see that if you select the transition Exit Time is the default. The last state that we need is lowering the weapon. So drag that on now. You’ll find it naturally in humanoid_weapon_lower. The condition that we stop shooting and lower the weapon is that the player is no longer in sight, so this should be the condition for the transition. Right click on WeaponShoot and make a transition to WeaponLower. now select the transition and set the condition to be the Player In Sight is false. Finally once we’ve lowered the weapon we want to transition back to the empty state. Right click on WeaponLower and make a transition to the empty state. Yet again we simply want the WeaponLowering to transition back in to nothing so we will leave the Exit Time as our condition. The last thing we want to do in the animator is make sure that the enemy grips his gun. The animations that we have don’t naturally have a grip posture for the right hand so we’re going to make a layer to handle this for us. Create another new layer by pressing the plus icon and call is Gun. So select the Animators folder in the Project panel and click Create – Avatar Mask. Name this one EnemyGunMask and expand the body mask. This time we want to deselect everything apart from the right hand. You should be aware that the body mask is represented by a character that is actually facing you. So the right hand of the character will actually be the left hand in the display. Now we can apply it to the gun layer by clicking on the small circle to the right of the mask property. Select EnemyGunMask and close the window. The only animation that we want in this layer is the gripped animation that we have on the robotGuard FPX. This is just a single frame of animation with the hand in a gripped posture. In the models folder expand char_robotGuard and you’ll find a Gripped animation. Drag and drop this in to the Animator. This is orange, which means it’s our default state, which is what we want. Also note that the blending of this layer is set to override and we know that our EnemyGunMask only has the hand visible. So we know that at all times the enemy’s hand will grip the gun. Now that the animator controller is finished we can drag it on to the Enemy’s animator component. Reselect char_robotGuard in the hierarchy and then drag and drop EnemyAnimator on to the Controller property in the animator component. Finally save the scene and the project. Note that at this stage our parameters don’t have any way of receiving information. So we cannot yet preview how the enemy will behave in the scene. In the next assignment we will be making the first script for the enemy. The EnemySight script will handle identifying the player’s location. Subtitles by the community

Only registered users can comment.

  1. Great tutorials. I noticed you changed the playback speed of the WeaponRaise animation to 3 to make it play faster. How would it be possible to increase the playback speed of the WeaponShoot animation during runtime? or is it even possible?

  2. i want to keep going! these are being uploaded every 2 weeks. at that rate unity 5 will be out by the time this is done.

  3. I think it would be helpful to include workarounds for those of us who do not have the pro version of Unity. Following these tutorials, I have found a few instances that use pro features, such as the IK Pass in this lesson.

  4. The animator controller is really cool. I'm assuming the 2d field you move the dot around on has, in this case speed in the Y axis, and angular speed in the X axis.

  5. Great tutorial about Mecanim, I wish Unity would spend more time explaining about IK pass. Unity Learn tutorials seem to avoid talking much about Pro features, as a result, Pro features most of the time, are in the black box area, only Documentation covered. As a Pro user, I wish more in-depth tutorials about them.

  6. Why do we need 4 different (A-D) animations? Can't we repeat the A one twice for B, 3x for C and 4 times for D?

  7. So it is impossible to make robot raising a gun and pointing a player without Unity Pro? Or it is free substitute?

    Btw: why I can open this project and it is working with raising gun? I have just Unity Free.

  8. There seem to be a lot of negative comments here for some reason.  I just want to say that these tutorials are clear, precise and well made.  Thank you!

  9. Is there a workaround for the IK pass if using the free version of Unity 4.6? Is it available in Unity 5?

Leave a Reply

Your email address will not be published. Required fields are marked *