In Unity5 there are often many different ways to accomplish your goals. Additionally there are many different things you can do but it isn’t always obvious that they are possible or how to do them. This page shows you implementations of things that can be done in Unity3D v5 which is why I subtitled this article: How to do shit.
Inversion Of Control
If you want to do Inversion of Control or Dependency Injection in Unity this is how
I was looking into available “tweening systems” this evening and found more than I bargained for:
iTween is the most popular but everyone complains about it having garbage collection problems.
DoTween and LeanTween are the runners up in the popularity contest but they don’t have any GC issues and are probably better choices overall
DoTween is documented better and some say is laid out better and is perhaps a more “complete”package but it requires that you get comfortable with lambda expressions
LeanTween has less documentation and examples but many people say it is straightforward, and incredibly fast
They are both free and open source
Theres this package called “Movement Over Time” which apparently does things different than a tweening system and is “more like a whole new way of thinking of and doing movement in your game” (from one of the reviews on the asset store). the same reviewer also said “I am amazed by all the new things I can do in only a couple of lines of code in this framework that seemed impossible before.”
Then I learned about Animation Curves which are built in and can apparently be used in the context of lerping to make that process entirely easy: transform.position = Vector3.Lerp(fromPosition, toPosition, animationCurve.Evaluate(t));
I could totally type these in for you but I choose not to because I don’t want to and because there is inherent brain/learning value in typing them in yourself and I believe you will get more value out of this page if you type them yourself. I type them in again every time I use them. Start typing or find the answers somewhere else.
How to do something periodically
The easiest way to do this is with “InvokeRepeating”. The second easiest way is with a coroutine. But if you want to do it with code in the update method:
How to Drag an object with the mouse
How to quit your application
How to affect a bunch of GameObjects at once
How to do Collision Detection
How to add a menu item to Unity
Cycling through an array the easy way
This is how to set an object to a different texture every time you press the space bar.
I downloaded the lite version the other day and spent some time tweaking it this morning and decided that it had enough features and quality to go ahead and pull the trigger on the Pro version (its only $10.00!?!)
Importing the assets
This video is a virtual unboxing of the FPS Starter Kit Pro.
I import the assets, fire up the demo game, and then modify some parameters to make it more playable so we can see what it has in store for us.
Fun Easter egg: near the end of this video my mouse disappears. It wasn’t there when I was looking at the screen but the screen recorder superimposed it back in. I was moving my mouse blindly trying to click on the exit button and without knowing where the mouse was I was able to hit the exit button twice! #mousingpride
Making a Few Improvements
How to exit the game
How to make enemy spawning more consistent
Adjusting Weapons (and other player settings)
Applying the FPS Starter Kit in an Original Scene
Reloading the current scene when you die
Waves of Enemies
Finishing a Level and Switching to Level 2
Level Advancement From one level to the next
Get Pause Working
Timeshift (making the game run slower or faster)
Short Pause when Level Complete
Adding a text HUD (and fixing warnings in the bargain)
Fixing GUI to Fix Warnings
Fixing the Pause menu and Locking the Mouse Cursor
Making a Prefab from Player + GUI
Improving the looks of the Floor Texture using Normal Maps
Improving the looks of the Weapon using Normal Maps
Lighting was getting dark upon reload
Found out a Unity5 quirk: that this darkness (double-baking) is somewhat expected when we auto-bake lighting (which is enabled by default) for every scene we create we need to do one of two things: change the lighting from automatically baked to manually baked and then every time you change the lighting go bake it manually. Or live with it while we are editing. It apparently never follows a deployed build into the field so it won’t be a problem long term.
Making the Sun Rotate Around the Playfield
Well I’ve been using Unity FPS Starter Kit Pro for many weeks now and it has proven to be pretty good. Still there have been some annoyances and some of them are documented here along with other things I find as I use it.
Problem with Rockets
I have found an intermittent problem with launching rockets at your foes that is making me want to find a totally different solution (perhaps download another FPS kit or build my own rocket and/or AI implementation) since I really wanted to use rockets in my game.
As you can see in the above gif rockets sometimes just don’t hit the target when they clearly should.
Its not a collision bound problem. Its worse as the speed of the rocket increases but it is somewhat of a problem at any speed.
Problems with enemy Rockets
The Enemy doesn’t aim their rockets.
They are just shot out straight from their launcher (or at the same angle every time) which means if the robot model is bigger than you then it will basically only hit you with good luck. Sorry I can’t get a video of this problem at this time but just imagine the gif video with that robot shooting rockets at you and they fly all around you but none of them hit your or explode anywhere near you.
The Enemy blows themselves up.
If they are walking towards you launching rockets but there is anything in between you and them they will just keep shooting and hitting the object until as they get close enough that they are inside the explosion radius at which point *boom*. On a related note if there are two enemies spawning from the same location they will inevitably as they are both proceeding directly towards you, get in the line of fire of each others rockets and shoot each other down. I was actually play testing a level I made and found out that all of my 25 enemies had killed themselves or each other!?! Solution: I modified the rocket’s ‘physical explosion’ code and made it specific to the robots (previously it was common for the robots and the player) and added a check for whether a robot (tag of “Enemy”) was the collider that was within the explosion sphere when it got called back for the collision.
Its large and not that helpful. My tutorials here are vastly more useful for anyone who wants to start using this asset.
Plus theres the fact that literally half the size of the entire asset pack is what amounts to a 10 page PDF (with about as many words).
The official documentation is here but I can’t recommend that because I found this answer on StackExchange which very nicely sums up what to do, but I haven’t tried it yet because I got distracted by this cool site:
I was looking for a good .gitignore file for Unity3D projects and I found that a semi-official one is maintained here but I also noticed that there is a cool website that has .gitignores for tons of different types of things: https://www.gitignore.io/
In 2017 I used exclusively the command line git client but recently I’ve been using Git Kraken as a client and it has been working for me very well. I think the biggest advantage I can think of right now is having the tree of branches visible at all times.
Fork then do a Pull Request
Here are the best instructions for when you are working with someone else git repository:
On the main page for this tutorial I learned that this tutorial was the end result of a public speech at a conference. Every year there is a conference called ‘Unite’ where ‘Uni-tics’ (thats a portmanteau of unity and lunatics) (of which I am one) get together to talk and learn about Unity3D and in 2014 this tutorial was one of the talks at that conference. You can get the slides that they presented at the time from the main page (linked above) and here is a youtube playlist with all 10 of the videos.
Pro Tip: As you are watching the videos, if you want to watch them faster, youtube (desktop) lets you speed up the videos and since its so easy to pause them you can speed through most of the stuff, then pause and implement what they say to do, and then unpause and proceed with the rest of the tutorial.
Pro Tip 2: If you want to go really fast then turn on the closed captioning so you don’t even have to try to keep up with their high speed spoken words.
If you get stuck along the way please contact me. I would like to help. If you want to dig into it on your own then consider looking at the following:
the “upgrade guide” that is in the assets folder of the project created (once you’ve downloaded the project files) (its called “Tutorials-SurvivalShooter-Upgrade to 5.pdf” and you can’t see it from Unity so you”ll have to navigate to it through the file system)
the comments on the youtube videos (I’ve discovered that these can be quite helpful sometimes!)
Also it’s worth noting that all of these videos have youtube live transcripts so you can read what they are saying (roughly) and click on a line to jump right to that spot in the video. This lets you skip ahead or back up in the video easily.
What is this ‘Survival Shooter’ that this tutorial speaks of?
Sometimes a picture tells a thousand words.
Sometimes a moving picture beats a thousand pictures.
This tutorial as a whole has a lot of spit and polish to it. Its targeted at complete beginners and as such it starts off helping you get Unity3D setup the same way they have it so things are more likely to look and work the same. A substantial caveat is that these videos were posted in October 2014 so I predict that there will be some level of tut-rot but never fear my dear reader because thats what this meta-tutorial is for!
Where is my project data
The tutorial videos gloss over this completely (wtf!?!) so I’ve made a simple video to show you how to do it.
In the Upgrade Guide it says that you need to delete the directional light from the Hierarchy pane. That light comes as a default light in Unity 5 and didn’t in unity 4.6 and the lights that they have you import are perfectly suited to this game so the directional light just disturbs the lighting balance that they had developed.
Tip – Convert project panel to list view.
A good tip I just learned is that it is easy to convert the project panel to list view simply by dragging the slider down to 0.
In the video and the narration they speak as if there is already a folder in assets called ‘scenes’. Well in the project I downloaded there is certainly not a folder called scenes. The narration seems to imply that you shouldn’t put it in the assets folder directly but in a subfolder so make a scenes folder in assets if its not already there.
I don’t know if the bgm is supposed to play when I hit ‘go’ but it didn’t until I checked ‘play on awake’ (which they talk about in the video). I’m going to uncheck that now to match the settings in the video.
Just prior to actually adding the Animator one of the speakers talks a bit about .fbx files and how the animations are stored within the model. He also speaks of ‘retargeting’ a process in which you apply an existing animation to another skeletal model but in order to do this the model must have been through a process called ‘rigging’. I don’t believe we will be doing that in this tutorial (since this player model is much simpler and rigged ‘generically’) but this is how many models are animated after they’ve been created and I’m glad he mentions it here.
He explains that the Animation Controller is a state machine that controls when each of the animations that already exist in the player model are going to be played back.
Once again the folder that they say is there is not so you will need to create a new ‘Animation’ folder in the project pane.
There is a Discrepancy in the settings
I just noticed something possibly slightly disturbing:
Both ‘Culling Mode’ and ‘Apply Root Motion’ are different! Lets see if there is anything about this in the ‘upgrade’ document in the root folder of the project… Aha! yes it does address ‘Apply Root Motion’ and states that it won’t matter. For the purposes of this tutorial I am going to assume that they also believe the difference in culling mode won’t matter and I will proceed with the tutorial.
Adding Animation Parameters
The upgrade guide mentions that the parameters are no longer in the bottom left but in a tab in the top left.
At 9m53s he explains that a trigger is like a boolean but it resets itself back to false as soon as it has been consumed so it can be used to trigger things to happen once.
Important Note: For every transition in this tutorial you must uncheck ‘Has Exit Time’ (found that note in the upgrade guide AND in the youtube comments).
Setting drag and angular drag to infinity is an interesting approach that I haven’t seen before. This means the player will stop instantly instead of floating along at all in the direction they were moving.
Implementations learned in this section
Adding an animation controller
Viewing built in animations in a model
Importing animations into the Animator
Finding animations within an FBX file in the project pane
Adding animation parameters
Transitioning between Animation states
Adding a rigid body for physics
Coding the Player Character
(same video ~19m10s)
This section speaks about some new things:
FloorMasks LayerMasks and RayLengths
FixedUpdate vs Normal Update
This seems to be an advanced and mature way of controlling the character and I am probably going to use this in other games.
Wow! By the time this video is done movement works and its pretty darned cool seeing the player character bumble and jump and twist like that!
Implementations learned in this section
A great way to control a player based on raw inputs
Rotation based on the ray cast between the player and the mouse
Topics and Implementations touched upon in this section
Camera following the player
Smoothing (so the camera movement isn’t quite so abrupt)
Vector3.Lerp to help with smoothing
Time.deltaTime to make your game frame rate independent
The Scripts/Camera folder doesn’t exist so you’ll need to create it.
Note: You must set ‘clear flags’ while you are here to ‘solid color’ (see the upgrade guide for a bit more info)
Time.deltaTime to me doesn’t makes sense (it obviously works I just don’t know what it really means). I’m going to look it up with CMD or CTRL + ‘ … ok I learned that it is the time since the last frame (context sensitive to Update or FixedUpdate) so this would be some number of … microseconds (I presume)? Ok this screenshot from the documentation helps a bit:
I found the difference between a normal collider and a trigger collider interesting in that the trigger collider doesn’t take place in any physics interactions. Instead a function is called behind the scenes whenever it collides with the player.
The Nav Mesh Agent section made me wonder what would happen if I didn’t change any of those values … I might try that later (after this all works).
The layout of these navigation panes both looks slightly different from the video.
Note: The Upgrade guide says that you must set Step Height to 0.34 and Manual Voxel size to 0.25. There is no width inaccuracy anymore.
I am going to remember that anytime you change the position of static objects in your scene you will have to re-bake the navmesh for things to work properly. That seems like something that could easily ‘bite’ you in the future.
Problem: Not being followed
In the video the bunny is following the player but mine is just ‘walking’ in place. Lets see if I did everything … ah there is an error in the code! Actually as I look deeper it wasn’t an error in the code per se. The code was looking for a game object with a tag of ‘player’, so I had to tag the player with that tag.
I went back to video 2 and confirmed that they DO tell you to tag your player with the player tag. Mine wasn’t tagged so it was somehow my mistake. I either missed doing it or I did it while I was in play mode (which cancels out everything you did when you exit play mode). Done. Fixed. Moving On.
Topics and Implementations addressed in this section
I wired up a way to cause myself to take damage (just so I could test it out) and I noticed that even after I was dead I was taking damage and making “ow!” sounds. I checked to see if I was dead before taking damage and that fixed it.
Problem – Slider is not working
I went back and looked closely at what I had done compared to the slider in video 5 and found the problem.
The problem was that the Min Value was set to the same value as Max Value. As soon as I changed Min Value to 0 it all worked!
Topics and Implementations addressed in this section
Wow. This is the longest video so far by far. Its an important topic but having never watched it (as of the time of the writing of this sentence) I wonder what kinds of things are we going to be doing that would take 40 minutes!?! I guess we are about to find out!
Ok the first 10 minutes is a walkthrough of code they have already written for you which will give health to the enemy (aka the ZomBunny) but I found it interesting that when you view the animations for the Zombunny you find that it just calls a public function called “StartSinking” whether it is there or not.
Then they spend way too long explaining block comments which is a pretty standard programming thing. I realize that not everyone will know what they are and that some people will benefit from them, but they have assumed a certain level of experience thus far and could have stuck with that IMHO.
Major Discrepancy: Line renderer
Well when it comes to changing the ‘parameters’ of the line renderer … well … there ARE no ‘parameters’!?! So I’ll try to figure out what to do instead. ok I’m back and I’m still not sure what to do so here’s what I did: I made Width = 0.5 and dragged that little red dot on the graph down to 0.1. When I tested the work we did in this section that looked just fine, but there might be a more ‘authentic’ way to do it (so to speak).
After this they walkthrough a script for 15minutes then they test the game. A few things I noticed: My gunshot sound is quite a bit lower pitched than theirs. Also my player shoots much slower than theirs. The gun sound doesn’t matter, but the player shot speed does; I fixed that by going to Player->Player and changing time between shots from .15 to .12 (its a very sensitive value).
P.S. The error at the bottom of the screen about a navmesh is deliberate (they mention that in the video but I wanted to give you a heads-up!)
Overall this was a pretty easy section without too many problems.
In this section they are making a score UI and setting it with values when you destroy the enemy. It talked about static classes and class data and when that will be useful in this section. I encountered no problems during this section and I think it was well done.
Use of Static Variables
This one starts out with a puzzle: The prefab they are suggesting that we use isn’t there!?!
At around 1:00 in the video is suggesting for us to use the Zombear from the prefabs folder but in my prefabs folder its not there!?! Well that is true and this is a legitimate small error in the tutorial but I found it somewhere else: in _CompletedGame/Prefabs its in there.
Similarly the “Character” they refer to are not in the “Characters” folder but instead in the “Models/Characters” folder.
At about 9:00 in it shows you how to spawn new enemies and I feel like this is a particularly elegant solution in a number of ways. It lets you have multiple spawn points, invokes a function regularly (I honestly didn’t know about that trick), and stops trying when player health is zero. It also is modular so you can drag more than one copy of this script onto your enemy manager to spawn different enemies.
At the end of the video I encountered a problem because one of my enemies was moving around but the other two weren’t. hmm …I’m going to try not spawning enemies but simply placing one in the game world and see what happens. Ok I solved it by removing the “enemy Health” and “Enemy Movement” scripts and re attaching the components from our scripts folders. For some reason that worked. I suspect it was using slightly different (and broken) scripts from the _CompletedGame/Scripts folder. Sure enough they are different in one fundamental way: they use a different namespace. For that reason you will need to copy over the Enemy Attack script as well. If you want to go all the way you will need to also add the death sound to the enemy health script. When you do the settings will be different:
Yeah replacing all of those scripts seemed to do the trick. and the game actually works really well now and is pretty fun!
This is a really cool section. In this section we are going to make a cool screen that overlays the game showing when the game is over but more importantly I think they show us how to animate any property of anything in the game!
The process to create a new animation has changed: In the video they refer to something called “Add Curve” now its called “Create”:
After that they show you how to set all of the values which is straightforward, then they drag a script onto your HUD Canvas, however the script they show in the video has one important thing that the script included in the assets folder does not: a restart timer to reload the level after a certain amount of time has passed.
If you want to add the restart time then this code should do the trick:
(note lines 4 and 26 in my code which are significantly different than the video)
I tried running my game without that restart timer code though and everything worked just fine! The game even restarted after a second or two! That must be an artifact of how they designed some of the other components of the game but boy it works great!
Animation of any public property of anything in the game
Doing something after a certain time has elapsed
Triggering an animation to begin
This was an incredible tutorial. You end up with a fun game and with understanding almost everything that they did to make it. I’m really impressed with the quality but I still saw quite a bit of tutorial rot so I’m glad that I struggled through and documented what I found here for you. Hopefully it was of benefit to you and I hope you learned a ton from the tutorial and got less frustrated thanks to my meta-tutorial. Best wishes and let me know if you have any questions or comments!
Unity3D – Implementation Roadmap (aka What Do I Need to Make a Game?)
Whether you are just getting started using Unity3D or a veteran Unity developer who started in the mid-noughties (get it?) there will inevitably be something you want to create which requires skills and techniques that you don’t yet have. Thats just what tutorials are for! There are a LOT of tutorials out there for Unity3D and in one of my other articles I am keeping track of some of them so that we can best leverage what they have to offer, however even with all of the resources that provides I feel like there is something missing …
What do you need to make a game?
You need implementations. Pretend that you would like to make just one level of a Super Mario Bros style platform game. You will need to know how to make Unity3D do a certain number of basic things before you can really expect to complete the game:
displaying your character, control of the character in 2d, walking, running, jumping, affecting movement in the air, friction on the ground, collision between the top of your character and the bottom of a block, death upon collision with an enemy unless you stomp on the enemy or unless you are temporarily invincible,
enemies roaming with varying movement patterns, growing your sprite or changing its colors when you get a power-up, popping power-ups out of blocks, moving the power-ups around in different ways, mushroom collisions with the environment, popping coins out of blocks, making question mark bricks shimmer,
timing the level, keeping track of score, death when falling into a pit, enemy death animations, character death animation,
stomped turtles turning into a shell, shell movement and physics, collisions between your feet and the shell, determination of which side of the shell your feet have collided with (to deterimine direction), collision with flagpole, sliding down flagpole, fireworks , animating dropping into pipes, switching to dungeon screens, animating crouch, shooting fireballs, fireball movement, fireball collision with enemies, etc.
That is just off the top of my head and there is undoubtedly more that I didn’t think of (and I didn’t get into sound or music at all).
Implementations + Content = Game
I’m going to start calling the ‘things’ above ‘implementations‘. An implementation is something you need to know how to do in order to create a game. Each of the actions, skills, tricks, simulations, activities, animations, collisions, conditions, sprites, characters, projectiles, or whatever is an ‘implementation‘ of something and when you combine some of these implementations with content to make it look and sound interesting you get a game.
The above list of implementations was targeting a Super Mario Bros. style platform game, but for every other type of game there will be a similar list of implementations. Perhaps the list will be longer, perhaps it will be shorter. Perhaps the implementations will be more difficult to implement, perhaps they will be easier. Perhaps there will be tutorials for them, perhaps not. Perhaps you will wish to make new and original implementations for gameplay that hasn’t been done before, or combine existing implementations in new ways from which original or fun gameplay emerges. In any of these cases you need implementations.
How to find implementations
One way (not recommended) is to overwhelm yourself with information. You attempt to learn everything that anyone might ever need to know about Unity3D and then after you’ve ‘mastered’ it all you can begin to build your game since you will then know how to make all of the implementations on your own and you won’t need johnmcgarey.com anymore. That is perhaps the slowest, least effective, and most frustrating way to approach game development. Additionally if you ask 12 experts they will all have a different list of things you must learn before you start developing games in Unity.
The way to find implementations and really make a game is to figure out what you need to do and then watch tutorials and read questions on forums and try stuff until you figure out the solution. The rest of this article will help with that.
Implementations that I have found so far
Ending the game and allowing the player to restart the game
When you begin to build a game in Unity3D or any other engine, Framework, or SDK you are likely going to need resources to use (unless you want to build everything yourself which is admirable but can take a long time) so here is a running list of the best resources I have found so far.
There are getting to be a shit-ton of game engines out there in the world and some of them can demonstrate really freaking amazing technology, but there are some things to consider above and beyond the technologies that the game engine advertises and provides and the single most important of those imho being an extensive, significant, and engaged user community. Without that you will have to figure everything out by yourself, and perhaps more importantly, if you have a major problem with the engine you could be the only one complaining to the developer. With an extensive user community those things become less likely.
A few things:
If an engine is not being regularly maintained I don’t want it on this list.
Just because it has 3D support or 3D acceleration doesn’t mean its a 3D engine
To be clear those things are fantastic and important regardless of whether I label it as a 3D engine.
A big part of my analysis about these engines is the quality of what has already been made in them.
Modern Full-3D Engines with significant user communities
* Meta-Tutorial – a walkthrough of a tutorial which shows the ins and outs and ups and downs of the tutorial and proves out that it isn’t too ‘rotten’ (see tutorial-rot in my other articles)
Part 1: Setting up the project
In this section Aaron talks about why he is creating the video and downloads a great character called “Unity-chan!”.
Warning Pop Up:
He shows you how to download Unity Chan but I noticed one thing that happened for me but not for him: I got a warning pop-up from the asset store indicating that Unity-Chan was created for Unity 4.6 and therefore some scripts may not work with Unity 5.
I am going to choose to ignore that warning since Aaron has made this tutorial specifically with Unity 5 in mind so I believe he will address most or all of the compatibility issues necessary to complete the tutorial.
Unity-Chan has her own EULA (End-User License Agreement)!
I bet if I keep my eyes peeled for this I’ll see more examples of it but I haven’t previously seen an asset having a license that they made just for that asset and to me that is kind of cool and it means they take their asset seriously as a product in and of itself.
I like how Aaron is showing the steps clearly and fast and not wasting time waxing poetic about his opinions about what is going on and why which should allow him to get through more tutorial content in a tighter video package and I appreciate that. He also edits the video to remove any dead space that was previously there.
In this section we are asked to make an ‘animator’ to control the animation. This is entirely new to me so I’m glad he explained adding the animator window. He’s walking us through this with an excellent understanding of what exactly we need to know.
At this point while you are in play mode (and once you have unchecked maximize on play) if you right click on any of the wait animations in the animator window and select ‘Set as Layer Default State’ you can see that animation take over instantly. Unity’s real-time response to changes and updates like this is remarkable and powerful.
He shows you how to transition from one animation to another and how to set a default, and then how to write a simple script that will soon let us control our animations but when he is compiling and running the first script at first I was concerned about all of the warnings I saw in the console but I see in Aarons window that he has many or all of the same warnings which I believe come from the Unity-Chan scripts not being completely up to date with the latest version of the API and everything works fine.
Here’s a simple ‘gotcha’ thats worth looking out for
I noticed that the first time I dragged the script onto my unity-chan character it seemed to add that component to the character but the next time I hit ‘run’ the script was no longer attached. This was because I had dragged it while I was still in ‘run’ mode and changes made while you are in run mode are not maintained. Once I dragged it over when I was not in run mode it attached and worked well.
What can you do with those animations
He explains what the final parameter is in the anim.Play command and shows what happens when you change that value to 0.5f. He also shows how to get input from keys, the mouse, and from the default input controller (no matter what that is configured to be) and how to use that information to trigger different animations, even choosing an animation randomly from between several possibilities. He shows how to pass floats via public attributes in the script through to the animator and how to see them update live as you press your input keys
I experimented with the ‘mirror’ property in one of the animations and it seemed to flip left to right but also to start the animation at the end and work backwards.
He shows how to create a new state machine from a blend tree and how to descend in to that state machine and then how to make a 2d simple directional walk animation and then adding a ‘motion field’ and how to use that field.
Once he got the wait states working he added a script to move the character based on all of the directions. He showed how to change the character to a physics object (added a rigid body) and how to add forward and backward motion and limit velocity when the character isn’t being animated. He showed how to add run animations based on a boolean parameter which was based on having the shift key depressed and how to make her move faster when the shift key was depressed. Finally he shows how to implement a jump and then slide animation.
Overall this was an effective and tight tutorial showing everything clearly and without fluff or wasted time. It gives you a good understanding of the Unity animator state machine system and with this you could code up some really complex and interesting characters. Even though the tutorial was made in 2015 there really weren’t any stumbling blocks and certainly no show-stopping issues and I am grateful to Aaron Hibbard for creating this video.
Thank you for reading and please check out the other tutorials and meta-tutorials on this site.
Observations from the Unity3D Space Shooter Tutorial using 2017.1
I’m starting this Space Shooter tutorial now and I’m going to log my comments, thoughts, impressions, and observations here in order to help others (and my future self) best leverage this material. The Space Shooter tutorial was created by the Unity3D team in 2013 using the latest version of unity at the time. Since the tutorial is four years old it has experienced substantial “tut-rot”* so in this article I’m going to use my Code-DeLorean to bring this space shooter into 2017 and make it work with Unity3D version 2017.1. Since this is in some sense a walkthrough of a tutorial, I’ll call this a Meta-Tutorial*.
* Tut-Rot – Tutorial Rot – The incompatibilities created as a result of toolsets upgrading while tutorials do not.
* Meta-Tutorial – Just coined it! See above for the explanation.
* Code-DeLorean – Bringing old code up to speed with new tools.
For Further Information
If you have trouble that isn’t addressed in this article, visit this thread on the Unity Forums which has 53+ pages of Q&A all entirely specific to this Space Shooter Tutorial. There is also a Unity 5 Upgrade Guide which is linked from the main tutorial page which describes some of the differences between the video and the current version of Unity, however its … well … spotty with messy formatting, missing letters and words, etc. I’m guessing it was based on a poorly done export of the youtube annotations of the tutorial videos, but the guide is certainly more explanatory and helpful than the annotations alone.
Lets Get Started!
While in this article I’m being picky about the details of the tutorial, I am actually a fan of this tutorial (evidenced by the time I’m taking to dig into it and write this article). I am glad the author created it and I think that combined with this article the tutorial will be useful to you. Lets Get Started!
Gathering the Assets
On the main Space Shooter Tutorial page: there is a link which purports to let you “download the assets for free” however sometimes that link inexplicably (though reproducibly) takes me to the main asset store page from whence I must search for “Space Shooter Tutorial” to get to the intended location.
Afterwards there is a link to “Download” or “Open In Unity” and a process of putting the content into a download manager queue, downloading it, then opening a new project with the new content you’ve downloaded. THEN from within the new project it asks you to download the content again.
There are various dialog boxes and buttons to click and none of it is too complicated but all told I found this step confusing and potentially frustrating and it delayed me to the tune of 2-3 minutes. Still in a good mood (probably since I just got engaged to be married … YAY!) I proceeded to the next steps.
This video goes into some detail about how to create a blank project. There is a short section about importing the Space Shooter Tutorial content which has rotted entirely and it doesn’t seem to show anything about how to do that part so you’ll have to poke around a bit. It shows how to change the screen resolution of your final published game and also has some good discussion of build settings and of editor layouts and how to save them.
A slight discrepancy between the video and the latest version of Unity3D is the non-existant “project creation wizard” that they speak of. A totally forgivable Tutorial-Rot problem which I think most people will be able to figure out.
From the Video:
From the latest Unity 2017.1
Note: Since I hadn’t installed the WebGL platform when I installed Unity I had to do that during this step. There was a convenient link in the Build settings which let me download the package for Mac, and the installer worked well even when I had Unity still open, however I had to restart Unity for it to really recognize that it could publish to WebGL.
This section has a number of youtube annotations that provide corrections to the instructions given in the video and although most of them aren’t that critical, Youtube annotations aren’t typically visible on mobile devices so keep in mind that you may be missing a few things if you are watching on your phone most notably:
In my case without ‘convex’ selected, the mesh collider didn’t even show up in the scene view!?!
This video adds in three directional lights to make the ship look like it is lit from a bright star off to the right but also enough ambient and ‘rim’ lights to be able to see the ship clearly during gameplay.
When this part talked about removing the directional light and skybox I got momentarily lost because the ambient lighting menu is VERY different now, however one of the annotations in the Youtube video pointed me in the right direction: Window(Menu)->Lighting->Settings. From there although he talks a lot really all you need to do is make the ambient lighting totally black.
Late in the video an annotation pops up suggesting that due to changes in the engine’s lighting pipeline you should set your lighting intensity to about double what is suggested in the video. I had to go back and set the main light to 1.5 instead of 0.75 and when I did everything looked much better.
This video shows how to add an image (aka texture) to a flat mesh called a quad to create a background and also speaks of how to properly arrange, and scale that mesh, and how to light it without affecting the lighting that applies to the ship. At the end we move the background down and out of the way of the ship which interestingly doesn’t affect how the camera sees the background since we previously set it to ‘orthographic’.
Now we’re finally adding some gameplay. I feel like this would have been cool to have going in the first or second video because its motivational to see things actually happen in game. This section uses a clever (though ultimately really simple) mapping of input values to velocity to move the ship around using physics.
Its impressive to me how easy it is to get the ship moving. This video talks about how to speed up the movement of the ship using public variables and how to set those inside of the unity editor (so you don’t even have to touch the code to change the value) by exposing the public variables to the Unity Inspector. One thing I noticed was that you can actually change the speed value WHILE you are playing the game to test out different values and see which one you feel best suits the gameplay. He shows something cool about [System.Serializable] which is a magic word that exposes public variables of a class to be visible in the Unity Inspector. This is imho an AWESOME feature which enables a tight integration between the code and Unity Engine GUI.
In this section he reminds us that to get to the scripting documentation when in Monodevelop is CMD+’ on the Mac.
There is a critical annotation here saying that we have to use “GetComponent” explicitly even though the code in this section doesn’t do so. This is addressed somewhat in the “Unity 5 Upgrade Guide” that is linked from the main tutorial page, however in that guide the code has a typo.
private Rigidbody rb;
Not a big change of course but the code won’t work without it.
This section talks about creating a material for the bolt that your ship is going to shoot and then adding the script to send it flying forward on the screen. Its not yet hooked up to the ship specifically but just launching from the origin point. Pretty early on in this section I got stuck.
It said to just click on ‘select’ and I could select a texture for my material but tut-rot strikes again and there was no select button. It gave me the apparently helpful hint that “Albedo” was what I was looking for but I don’t see a select button on that either!?! Referring back to the Unity5 Upgrade Guide I found nothing of value.
I tried dragging the texture onto the sphere at the bottom, and tried dragging it onto the Albedo field with no success. I also tried dragging the texture onto the material in the Project Browser but no luck. I jumped the gun a bit when I dragged the texture onto the VFX that we just made in the Heirarchy and it looked pretty good at that point, however that didn’t help with the point of this part of the tutorial: making a new material with an associated texture.
Eventually I figured it out! When looking at some of the documentation I noticed a texture in a place that I didn’t expect: I didn’t realize that the little check box to the left of the Albedo property is actually not a check box at all (I was wondering why it wasn’t checking when I clicked on it!?!) Instead it functions as the texture box and the even tinier icon to the right of that box brings up the select window. Having learned this lesson I will never have a problem with this again and will always see those “checkboxes” as “texture boxes and selectors”, but that was less than obvious and I don’t feel bad for not having seen that right off the bat.
This lesson discusses the difference between FixedUpdate() and Update() in scripts. Thats something that has been discussed before in these tutorials and basically the difference relates to whether physics are involved (FixedUpdate()) or not (Update()). It goes into quite a bit of detail about exactly what the code does (possibly TMI).
In the process of getting this working they show you how to create a constant stream of shots and although they don’t show this in the video its pretty fun to play with!
Note: I left gravity on my bolts on purpose to see what it would do 😀
I tried something here: I tried to make my shots veer to the right or left by adjusting velocity. It didn’t work so I asked my fiancé for a failure high five!
This section is about making your shot game objects delete themselves when they get outside of the play field. This is so we don’t make too terribly many game objects that never get deleted.
Before dinner today I took a stab at this and it totally worked!
However the way they are going to do it is more elegant and useful. They are making a boundary box into a trigger collider so they can test for objects to cross that boundary and therefore be destroyed. In one of the previous episodes I had left the gravity on the bolt, but at this point I removed it since the bolt was leaving the boundary box early (through the bottom actually).
This tutorial shows you how to make an asteroid. It talks about the Random.insideUnitSphere function which returns a random vector 3 value which we can use to set the angular velocity of the asteroid (making it rotate).
It talks about destroying the asteroid by programming some trigger collider interaction.
I liked the narration around 9m30s. He finds a bug, feigns surprise, and then says “No we aren’t standing at a flux point in the probability field. We have a bug.”
In this tutorial he shows us how to make the asteroid move, and explode which will effectively make it more interesting. It shows how to instantiate an explosion GameObject at the location and rotation of the asteroid which makes a pretty effective explosion effect imho. It instantly added a level of fun to the game and made me want to have more asteroids to blow up (which I’m pretty sure they are about to show us).
This tutorial also shows how to make the ship explode when it hits the asteroid. That is in fact a built in feature of the scripts as we have them written but I believe he is about to special case the ship explosion to make it more interesting (and so we can keep track of the score).
I noticed something: When you make a public class variable and don’t set it in unity, the execution effectively ends instantly. Example: I just added a playerExplosion public variable but didn’t set it and now my collisions don’t work anymore. It didn’t just not-instantiate the explosion that I didn’t set, it just gave up!
This guy is claiming that one asteroid per game is too easy … ok he might be right. This video will spawn waves of them. In this video he is a bit judgmental about code he deems to be ugly … ok he might be right about that too. :p
In this section he spends a lot of time on a ‘for-loop’ which is a standard programming thing that imho he could have skipped over or referred to another resource, still he did a great job explaining it!
Wait! That is a legitimate problem
Problem: that problem in the “asteroids out of control” gif above (the one where the asteroids are deleting themselves for no reason) that is much more significant now that I’ve got fewer of them and its not happening in the video. This means I have a bug in my code somewhere and I have to debug it. I’m going to try to do that now before I go any further.
I put a line of code in the DestroyByBoundary class to show me when something exits the boundary and that seems to be the problem:
But why? I’m guessing that the asteroids have a vector in the y direction and are escaping out of the top or bottom.
I noticed that there is only a problem when the asteroids collide with one another so one way to fix it *seems* to be to make the asteroid prefab into a trigger collider because then its physics aren’t interacting with other asteroids physics, but I looked carefully through the videos and immediately prior to making the asteroid prefab the “Is Trigger” box is NOT checked so it doesn’t seem like thats how they solved it. I’m starting to think they didn’t solve it but if so I don’t know what I did differently.
Anyway the way I decided to fix it is to constrain the x and y values to 0.0f in the update routine of the mover like so
This worked and I can shoot the asteroids and all seems to be well now.
Back to Waves of Hazards
It talks about how to suspend a routine without suspending the entire game. The solution is to make that part into a C# coroutine and there are just a few specific pieces of syntax required to do this (a good example is the WaitForSeconds code example). The “StartCoroutine” and the return value of IEnumerator are really the only parts that matter. The StartCoroutine makes sense to me but I don’t know what an IEnumerator is or why its required and I don’t think that is explained well enough but regardless of all that otherwise it is just a normal routine but now has the ability to pause.
Well I had a problem right off the bat here: Audio wouldn’t play! Even just from the preview window. 90s of googling and I found the answer on a 2012 forum thread suggesting that on the Mac if this happens its usually because Unity3D has been open for a long time (true in my case: several days) and restarting Unity should re-enable sound. It totally worked.
There is a good explanation here of what 3D sound does and how it relates to an audiolistener.
The tutorial suggested that you could drag and drop a sound file into a prefab in the inspector and it would create an audio source automatically and populate it with that audio file. I tried for 5 minutes and did not find any way to make this work (dragging onto icons, browsers, inspectors, different parts of inspectors, etc.), so I just ended up doing it without the automatic drag and drop audio source that they spoke of in the video. Manually adding an audio source component and dragging the audio file into the first field (called ‘audio clip’) in that audio source component worked like a charm and I’ve got exploding asteroid sounds now!
The drag and drop did work when dragging onto the hierarchy and I was able to attach it to the player object this way.
In this section we are shown how to display a score and keep track of points as you accumulate them. The first thing they show is how to Create a GUI Text component but the way to do that in Unity5 17.1 is pretty different than the way they did it back in 2013 so there is a large annotation that pretty much says to do everything different.
An important note when making GUI text in Unity 3D 5 2017.1 has to do with the ALT key. (UPDATE: I realized a bit later that while this is technically correct and I’m glad I figured it out, this doesn’t apply directly to this tutorial since I had done something wrong. See the bottom of this section for details.)
If you click on the box in the top left of the Rect Transform component of the GUI text it seems like you can place the text wherever you want just by clicking. You have to hold down the ALT key to while you select to do that.
This video talks about GUI Text and its properties and limitations and how since its GUI we won’t see it in the Scene view.
Theres a bit of a naming problem when adding the score in since the parameter name is “newScoreValue” but we are just adding it to the current score value. It should probably be named something like “scoreToAdd”, but I understand not always getting things right because as we all know naming things is hard. To be fair to this tutorial I’ve been impressed with their naming throughout and have had only this single exception which is a pretty good track record imho.
In this video they do a good job of explaining objects and instances using a “valet parking” metaphor, which works quite well in this context. They do this while they are showing us how to do script to script messaging by identifying and then calling the games specific instance of GameController from within another script. Of all the code in this tutorial the code that finds the gameController instance makes me the least confident. Not because it wasn’t written well, but because there are so many places it could go just slightly wrong: finding the object by tag (what if there are two), getting the script out of the object, etc.
I encountered a problem near the end of the video: It said to drag the ScoreText object to the Score Text slot in the GameController inspector but that didn’t work. I suspect it has something to do with the type I selected in the code and am about to go check the Upgrade to Unity 5 Manual for clues.
Oh! When I re-read the instructions in the youtube annotation (the one that I screenshotted and pasted at the top of this section) I realized that I hadn’t done exactly what those instructions said to do. I did Create->UI->Text from the Hierarchy however it says to create an empty game object and add a GUI Text component. I’ll go do that (I bet it will work) … yup! It worked!
Success! Wow that ended up being a tough section for me.
This video explains more about how the GUIText objects are positioned on the screen. Remember that 0,0 is the bottom left of the play window for these objects (see previous video). This video makes a public method that ends the game, and sets up the ability to restart by polling for the r key to be pressed in the update method.
This video speaks of using Application.LoadLevel() however the documentation shows that to be a deprecated method and suggests using SceneManager.LoadLevel() instead. Theres no Youtube Annotation for this so its good to know about it. It didn’t work immediately so I looked into the documentation and found out that it requires you to specify SceneManagement in the using list at the top of the file.
Thanks for walking through this adventure with me! The tutorial was more rotted than I imagined but also had a higher quality core than I first thought. Thanks to Unity for the engine and for the tutorial. What questions do you have for me?
Unity3D – Hitchhikers Guide to the Tutorial-Sphere
A curated reference list of the best and most effective tutorials about Unity3D
Introduction to the Hitchhikers Guide to the Tutorial-Sphere
Unity3D is a great tool but when you are starting out it is overwhelming. For a beginner it will be easier to pick up than Unreal Engine 4 because the way it does things is a bit more in line with the way the rest of the world does things, but even so it will take time to get used to even a fraction of what Unity3D has to offer. Fortunately if you are clever about the learning process you don’t have to ‘learn it all’ and by learning the right parts of Unity3D you will be able to make something useful or fun or special. This page is here to help guide you through the ‘tutorial-sphere’* and help you find the tutorials that are right for you as well as avoid tutorials which have fallen prey to the deadly ‘tutorial-rot’**
* I just invented the term ‘tutorial-sphere’! not bad eh?
** tutorial-rot (aka tut-rot) – the phenomenon that tutorials experience over time when they become so old or unclear that new tut-ees will just find them frustrating.
This is a good video by Unity3D College whose videos are quality because they explain everything that is going on, use discipline in their coding and show you shortcuts and best practices. In this video he shows use of the NSubstitute dll which once you have defined an interface for a class (which Visual Studio makes pretty easy) you can use to create a replacement instance or ‘mock’ which you can configure how you like. Its also a good example of running unit tests in Unity and getting around one of the biggest hurdles namely you can’t directly instantiate Monobehaviours (using new) to use in your tests.
On the Unity – Learn website there are a number of useful tutorials and the first one you will probably encounter in the Tutorials section is the ‘Roll-a-Ball’ tutorial and it is excellent. It only takes about an hour to go through, is detailed, fresh, and shows you a ton of what you can do with the engine and I have a feeling that the workflow that the author lays out here would be applicable for almost any project. He shows great tricks and best practices and you end up with a simple deployed game too so I can’t recommend diving into Unity without running through this tutorial. The one thing he doesn’t show how to do is change the screen layout of the Unity3D engine to match his. That is in Window->Layouts->”2 by 3″. Update: This is a text based version of this video tutorial which may be helpful to work with instead of (or in compliment to) the video.
Also on the Unity-Learn website is the Space Shooter Tutorial. The link above doesn’t link directly to the space shooter tutorial on the Unity-Learn website however a lot has changed since that tutorial was made so I have something better: A Meta-Tutorial of the whole Space Shooter Tutorial! This is a walkthrough of the whole tutorial but done using Unity 2017.1 and it will show you the puzzles that I encountered and how I solved them.
This is quite simply one of the best tutorials I’ve ever seen. note: I have not walked through this tutorial or done a meta tutorial on it, I found it and am blown away by the quality of the presentation of the whole thing. Its got text explanations for everything with links for things that are more complicated along with extensive screenshots covering every checkbox and field you need to fill in. All of the code that you need to change is highlighted as well! Very impressive.
This tutorial makes a portal shooter so you can shoot two portals and walk into one and walk out of the other (which comes from the game portal in case you hadn’t heard). He also shows how to mirror the view from the one portal into the other. Its a really cool effect to simulate and fun to do the tutorial and it takes less than an hour to follow along. This tutorial doesn’t talk about rotation and due to the mouse look routines getting that right is non-trivial so I just ignored that. This does a great job explaining how simple raycasting works. Note1: he claims in the middle that to get the mirroring effect you will need unity pro but you don’t. Note2: Near the end of the video he shows the dragging of the texture to the material, but the material pane looks different now so it still works but you just need to drag the texture to a slightly different place in the same pane: Instead of what he did drag the texture to the little box to the left of ‘albedo’.
This tutorial series is called Beginner to Hero and teaches all of the basics of Unity to total beginners. The author has a good sense of humor too for example: “Setting the Editor to Shaded Wireframes makes me feel like the projects I work on are more complex.” in another fun example when he gets something working he just says the word “Baller”. Between clever quips he does a fantastic job showing all the little steps in a clear and elegant way of how to get a game like this working well. Some of the code is hard to read (explanation here) but that doesn’t get in the way of the efficacy of the material.
This classic tutorial series shows you how to make a game they call “worminator” from scratch using unity. Warning: This tutorial is very old. It was one of the first that really went viral on youtube and has been acknowledged by the Unity team as important to the success of Unity in the early days. Due to its age (and therefore how outdated the version of Unity they are using is) I can’t recommend trying to follow along with it click for click however the concepts are explained really clearly throughout so its a good one to watch in full to get some more context for what can be done with Unity. They have some newer tutorials but I haven’t watched any of those, I just know about this old one.