I have started my next tutorial and thus continuing my TSSK plan set out on the 3rd April. Clayton’s Impossible Survival Shooter is a version of Unity’s Survival Shooter with some of my own design input. Although in this one I will try to limit my input just so that I can get through it sooner. I must remind myself not to get carried away when I still have much to learn. All assets have been downloaded from the Asset Store as part of the tutorial. I also will not be putting out blogs that are my version of how to build the game, instead I will just blog on specific topics, mainly on new skills and knowledge I have gained through building the game. That way they stay mercifully short (whew! about time, i hear you say?) but still hopefully informative and hopefully entertaining and interesting at the very least.
I have seen other students not only draw/create their own assets and/or sprites and wonderfully bring them to life with simple animation so this part is very interesting and important to me.
In the Model’s folder, in the sub-directory folder Characters select Player and drag into the scene, reset transform. In the Tag section in the Inspector, select tag ‘Player. In the Project tab create another folder and rename ‘Animation’, then inside that folder create a ‘Animator Controller’ and rename it ‘PlayerAC’, AC = Animator Controller.
Drag the PlayerAC onto the Player in the hierarchy so that it is attached to the player and in the inspector you will see the Animator component added.
The picture below shows you how it should look on the screen. Note that the PlayerAC does not show up as a child of the Player in the hierarchy.
Now you need to open the Animator window, this can be done through either the Window menu at the top, or the easiest way is to simply double-click on the PlayerAC in the project view. This will open up the Animator tab which can be docked beside the scene view if you wish.
Now if you go back to the Models folder and click on the expand arrow on the Player model so that you see in the Project view. Now whilst the Animator tab is still open drag
the Death, Idle and Move animations into the Animator window.
The Death animation is in orange as it was the first animation state to be dragged into the Animator window which makes it the default state, which is not want we want. So I right-click on Idle and select ‘Set as Layer Default state’, now Idle will be orange.
In the Animator window under Parameters tab click the ‘+’ button and select ‘Bool’, rename this Bool ‘IsWalking’, exactly how I did because we will need to reference this in a script/s.
Create a trigger parameter and name it ‘Die’. A trigger is like a bool as it can be set to a true or false state, and when we set it to a state, say ‘true’ the moment it triggers to false, it resets back to true, so that it can be used one time and then resets itself. So the Die trigger will happen when the player dies.
We need to now set up ‘Transitions’ so that we can go from one state to another, but we want them to transition logically. We want to transition from idle to move. To do this we right-click on the Idle and select ‘Make Transition’ and then drag the arrow to the Move state, it will turn white with an arrow pointing to Move. If you drag Move around, the white transition line will stay connected.
I then click on the actual transition arrow, which will turn blue and in the Inspector the properties for this transition will be displayed.
We want to look at the Conditions at the bottom of the Inspector and we want to add a condition by clicking on the ‘+’ button which will give you a list of conditions to choose. Select IsWalking. (which should already be displayed) and make sure that it is set to True.
Now do the same but transition Move to Idle so that the character can transition from moving to being idle. In the Conditions add IsWalking but this time set it to False.
This will mean that the idle animation will play instead of the IsWalking animation.
I have to now create a transition to Die, but I want the transition to die must be able to happen from any of the other states. So this is where I right-click on the Any State and select the only option ‘Make Transition’ and drag it to the Die state. In the inspector under the Conditions, I add the Die state, now as it is a trigger we do not have to set it to true or false as when the player dies, we just want the die animation to play immediately.
We go back to the scene view and select the Player and add a Rigid Body through add
component in the inspector. As we do not want the player character to slow down over time change the Drag and Angular drag to ‘Infinity‘, by typing inf in the boxes and pressing enter. We want to use gravity so keep that box checked and we want to use constraints so that the player character does not fall over in any direction or fall through the floor. We click on the Y axis in the Freeze position and the X axis and the Z axis in the Freeze Rotation.
We then add a Capsule collider to the player character in the inspector and adjust the properties so that the collider covers the player character nicely. I also want the player’s character to make a noise when hit so I have to add an Audio Source. In the Audio Source in the Audio Clip box select Player Hurt from selections available and un-check the Play on Awake box. Finally to enable the player’s character to do anything, say anything or to even function, add script called PlayerMovement. At the end of this the Player’s inspector should look like the picture above.
Open the PlayerMovement script for editing. My advice here now is to go to this link and see the script that needs to be created as it is a long script, simple but a bit longer than the scripts I have done in my previous games. Here are some highlights…
I had to create a CameraFollow script for the camera to follow the player character as it moved around the screen but I will not discuss here as this post is about the animation.
At the moment when the game is run, the player will rotate to the direction of the mouse pointer with the IsWalking animation playing which for this game pretty much looks like the character is just sliding across the floor, so its hard to tell what animation if any is playing. hopefully this will get clearer as I progress.
Well as it turns out that is the animation playing. So I guess there you go for this animation post. It is all rather simple, a lot simpler than I thought and imagined in the first place.
Essentially the steps are:
- Have the player tagged ‘Player’.
- Create an Animator Controller and add to the player.
- Open the Animation window and add the animation states from the player.
- Set up player with all components.
- Create transition states and set parameters.
- Create and add scripts.
At the moment only the Iswalking and Idle animation is playing, how do I set up the Die animation to play when the player is killed?
Adding Nav Mesh Agent to Enemy
Before I continue with animation I wanted to talk briefly about this Nav Mesh Agent as it sounds pretty cool and nifty because of what it can do.
Add the Nav Mesh Agent through Add Component in the enemy’s inspector. When you do this a Navigation tab is brought up docked beside the inspector tab. Go back to the inspector view and change the size of the agent, height (1.1) and radius (0.3). Change the stopping distance to 1.3 and change the speed to 3, leave all the other values as is.What does a Nav Mesh Agent do?Well glad you asked. Nav Mesh is a simple AI system in Unity. Through process called baking you can specify which parts of the level are navigable, in my mind I imagine them baked hard into the surface, creating solid structures that cannot be travelled through.A Nav Mesh Agent is licensed to kill, huh? Sorry imagination getting carried away. No a Nav Mesh Agent is better, it is something that can move through the level. It is a system that is like a pathfinder, it will pinpoint a target and then move towards that target whilst avoiding obstacles.
Select the navigation tab next to the inspector and select the middle Bake button for the display on the right. For this project the:
- Agent Radius sets how close the center of the agent can get to walls or ledges. Changed to 0.75 to allow for larger enemies.
- Agent Height set how low the spaces that an enemy can reach. Changed to 1.2 to allow for larger enemies.
- Max Slope sets how steep a ramp the enemy can walk up.I had to change as it said the Step Height was too small. I am not sure what this option does
- Step Height sets how high obstacles are that the agent can step on. Changed to 0.1 as this will allow the enemy to be able to step over any bumps in the floor.
- Advanced settings, don’t change anything. Voxel Size has to do with baking more efficiently and saving memory. Unity sets up appropriate values and only needs to be changed if you want a really accurate Nave Mesh
Once all values are entered press ‘Bake’ and Unity will process and bake, once done you are presented with the following screen….
What baking has done is made everything in the level easily traversable. Every object in the scene that was marked as Static will be deemed to be easily moved past by the player or enemies. Any small variations in floor levels will be negated and thus making the movement smoother and more fluid.
I added the animations to the enemy in the same way as I did for the player and created and added a EnemyMovement script so that when the game ran, the enemy will find the player character and move towards it whilst the enemy movement animation plays.
Now I have to add another enemy whose actions and animation will be similar to the first enemy but it will play a different animation or do a different action at times.
To do this you have to create an Animation Override Control. With the animation folder selected, got to Create and select Animation Override Control and rename it ‘EllephantAOC’.
Like I said before the AOC is used when an enemy acts and behaves like another enemy but when it attacks or dies, it will perform another action or animation. Notice it looks similar to the AC but has a ‘+’ in the corner.
Click on the HellephantAOC so that it is open in the inspector. At the top where the box labeled Controller drag the EnemyAC into that box and you will see three new boxes added in the inspector. This is the animation clips from the original enemy and now you want to replace these clips with the Hellephant’s clips.
To do this, highlight the Characters folder in the Project view and open, the Hellephant prefab. Now drag the three clips to their corresponding box in the inspector.
So there you have it, animation. Now let’s be clear here, the animation was already created by someone else. All I had to do was place it and script it. I would like to know how to make my own animations as well, but that’s for another post.
For the meantime I have learnt
- how to apply and run animations.
- I know my way around the animation window.
- I know how to apply scripts to the animation.
- I know what and how to add the necessary components in the inspector.
I think that’s it for now, thank you so much for your company and interest, I sincerely hope you got something from it. Until next time….
God bless you