Friday 19 December 2014

Unity Growing Pains

Unity is an amazing platform, and it continues to grow. Like many other game developers, I've switched over to Unity as my primary platform for game development. While Unity is one of the most user-friendly game development programs I've ever had the pleasure of using, it isn't free of growing pains. I've been working on overcoming and documenting these trials, and in this article I'll share my experience.
Related Posts
The Unity Inspector is where you'll see all of the information about the selected object in the scene view.
unity-growing-pains-understanding-the-inspector
If you create a cube and then select it, the Inspector will show its Transformcomponent, which will display the xy, and z properties of the position, rotation, and scale of said cube. If you have a more complex enemy that you've created, selecting it will display all of the information on that object, which could include any number of components you've attached to that object.
There's one catch: in order for a property to be visible in the Inspector, it must be made public in whatever script that property belongs to: if you want to see thefireSpeed variable of a component attached to your object, then fireSpeed needs to be declared as a public variable.
This is where the real power of the Inspector comes into play, and here yet another trick must be learned. Any property that can be seen in the Inspector can be edited for that object. If your script sets a default value for a property via code, it's very important to know that the Inspector value will override it. This means that if thefireSpeed variable had a default value of 4, and you set it to 8 in the Inspector, then the fireSpeed variable for that object will remain as 8, even if you alter the value to 4 in the script and save it.
(Note that setting the variable to a scope other than public—that is, to private or protected—will revert the property back to the script value, and will hide the value from the Inspector.)
The last trick to remember is that values edited in the Inspector while in the Scene View when the game is not running will save, whereas values altered in the Inspector while the game is running during live editing (yes, you can live edit variables while testing!) will revert to their values once you're done testing the project.
These few key points aren't too complex, but it's very important you remember them, as not being aware of all this can lead to some very confusing debugging.
Prefabs are one of the most important aspects of Unity, but they can be a bit confusing at first, as there are a few minor quirks to them that aren't quite apparent at first glance.
A prefab is essentially a predefined definition of an object—a blueprint, for all intents and purposes, much like a class in OOP. Prefabs can be composed of any number of game objects, graphics, components, and so on. They are best used when you want a lot of instances of an object, and may want slight alterations of that specific object.
unity-growing-pains-prefabs
If you have a bunch of prefabs in your scene, and make an alteration to the prefab definition, all instances of that prefab will be updated. Alternatively, if you make updates to a specific instance, then press Apply, all instances of the prefab (as well as the main prefab definition) will be updated to the configuration of that specific instance. If you have altered Inspector variables, or other properties, on an instance of a prefab, you can press Revert to revert that instance to the same configuration as the prefab definition.
One of the first things I struggled with in Unity, even after watching a ton of tutorial videos, was how to navigate the visual 3D space within the editor. I was, admittedly, a complete novice to moving around in 3D space without being locked to a character (such as when playing a first- or third-person game), but I still found movement a bit odd and complicated.
It's not at all bad once you get used to it, but having a bit more direction when I first jumped in would have been great, so here are some helpful commands to properly navigate the editor's 3D space:
unity-growing-pains-navigating-3d
  • Right Click Drag: Look around the 3D space freely, without actually moving.
  • Click Object + F: Focus directly on an object, moving the camera right in front of it.
  • Alt + Left Click Drag: Rotate around the currently focused object.
  • Alt + Right Click Drag: Various zooming mechanics.
  • Scroll Wheel Forward / Back: Zoom in and out directly forward or backwards.
  • Scroll Wheel Click and Drag: Freely move up, down, left, and right, in 3D space.
  • Arrow Keys: Move directly forward, back, left, or right.
Regardless of whether you're making a 2D or a 3D game, you'll want to become familiar with the fabled z-axis. Many classes and functions will make use of the z-axis, and while you may not want to alter an object's position on the z-axis, you will need to be aware of it and the role it plays in various functions.
The most common appearance of the z-axis, that really can't be avoided, is its use in the Vector3 class. Every object in Unity has a Transform component, which is what holds the information for rotation, position, and all related data. In Unity,tranform.position is a Vector3, which contains the x-, y-, and z-coordinates of the object.
So even if your game is 2D, you'll need to work with the Vector3 class, and you'll therefore need to be aware of this parameter. The easiest thing to do, in this case, is to either set the z parameter to 0, or to set it to the current z value by reading it from transform.position.z.
As I stated above, all movement-related information is stored in a Transformcomponent. Ah, but you can't simply write to the transform.position Vector3—at least not in C#. Instead, you have to create a new Vector3, set the values of yourVector3 (xy, and z), and then set transform.position to your new Vector3, as follows:
//We can't write to transform.position, so we create a new Vector3:
Vector3 newPosition;
newPosition = new Vector3(theX, theY, theZ);
//Set transform.position to the newPosition Vector3 object.
transform.position = newPosition;
While this isn't exactly complicated, it will definitely add to the pile of things to trip you up if you're not aware!
All in all, nothing above is particularly advanced, or very technical; it's just a bunch of information that beginners to Unity often miss while learning. Anyone can figure all of this out eventually, but it can take countless hours of debugging, Googling, and experimenting, and there's no need for that.
I've watched every single tutorial video on the Unity site, and while it's fair to say that I didn't absorb all of the information first time round, a lot of the above simply isn't mentioned, or isn't given enough attention to make it stand out. Learning everything I've learned took me quite some time, and hopefully this compiled list of information will speed up the learning curve for future beginners to Unity development.
What growing pains have you come across while learning Unity?

No comments:

Post a Comment