Unity: Developing Your First Game with Unity and C#, Part 3 October 2014 You’re still with me in this series. In the first article, I covered some Unity basics. In the second, I focused on 2D in Unity.
May 15, 2014 - save and load game data within Unity3D using serialization; use static. For this, let's create a C# script and call it SaveLoad. In other words, this line allows us to create unique files and then read from those files later. 3D Files; 3D Renders 3D Car Models 3D Shaders CG Textures OBJ Files. Creating simple car mirror using rendertextures. HOW TO – Right click at Project window, Create / RenderTexture – Rename “New Render Texture” into something else, like “MirrorRT” – Drag & Drop “MirrorRT” into 3D object (preferably some flat object) at scene view (it automatically creates [].
Now I get to my favorite part of game development—3D. The world of 3D is a truly magical place—amazing immersive environments, rich sound effects and beautiful visuals—even just a simple puzzle game with real-world physics can keep you hooked for hours. 3D games definitely add a layer of complexity over 2D, but by taking it piece by piece you can build up a cool 3D game. New project settings for both 2D and 3D in Unity support 3D. You can have 3D objects in a 2D game (and vice versa). What Makes Up a 3D Scene?
3D scenes consist primarily of three main visual components—lights, mesh renderers and shaders. A light is, well, a light, and Unity supports four different types. You can find them all under the GameObject menu. Experiment with adding the various types and changing their properties. The easiest one to light up your scene is a directional light, which is like the sun in the sky.
A mesh (or model) is a collection of vertices that make up the polygons that make up an object. A shader is a compiled routine that contains code to control how your object will show or interact with light. Some shaders simply take light and reflect it like a mirror; others take a texture (an image to be applied to your mesh) and can enable shadows and depth; and some even allow you to cut visual holes through your models, like a fence. Models are typically FBX or OBJ files exported from another modelling software package. FBX files can also contain animation data, so you might receive one FBX file for your model and one containing several animations. Several third-party file formats are also supported, such as the Autodesk Maya.ma format and Blender files.
You will typically need the third-party program installed on the same system if you want Unity to import these files, and then it’s simply a matter of dragging and dropping them into your Unity project, just as you would any other file. Behind the scenes, Unity will convert other file formats (upon import or detecting file changes) into the FBX file format. Asset Store I touched on the Asset Store in my first article, but in 3D games is where it’s really handy. I’m not an artist, and because this is a technical magazine, I assume most of you aren’t, either.
(If you are, please accept my congrats, you are part of a rare group.) But if I want to create a game with lush environments and old destroyed buildings, for example, it’s not a problem. I can buy what I need from the Asset Store. If I want 15 different zombies, I can procure a pack from Mixamo in the Asset Store. The potential combinations are nearly endless, so don’t worry about someone else’s game looking like yours. Best of all, the Asset Store integrates into Unity.
You can upgrade your packages by clicking Window Asset Store and then the bin icon. You can also check out reviews and comments to more easily determine if a particular item is good for your project, for example, whether its mobile-optimized or not. Desktop games can typically handle a lot more objects/vertices/textures/memory than a mobile game, although some of the newer chips make mobile devices today seem like Xbox 360s.
In a typical 3D game, many of the same concepts from a 2D game apply—colliders, triggers, rigid bodies, game objects/transforms, components and more. Regardless of the type of 3D game, you’ll typically want to control input, movement, and characters; use animations and particle effects; and build an imaginative world that’s both fantastical and realistic. I’ll discuss some of the ways Unity helps with this. Input, Movement and Character Controllers Reading input for movement becomes a bit more complicated in 3D because rather than simply moving in the X and Y planes, you can now move in three dimensions: X, Y and Z. Scenarios for 3D movement include (but aren’t limited to) top-down movement, where a character moves only horizontally and vertically; rotating a camera or character when reading mouse input, as is done in many first-person shooter (FPS) games; strafing left to right when reading horizontal input; rotating to turn around when reading horizontal input; or just walking backward.
There are a good number of movement options from which to choose. When moving an object, you don’t give it a position to move to, as you might expect. Remember, you’re executing code with each frame, so you need to move the object in small increments. You can either let the physics engine handle this by adding a force to your rigidbody to move it, or you can tween the object. Tweening basically means transitioning between values; that is, moving from point A to point B.
![Car games unity 3d Car games unity 3d](http://d2ujflorbtfzji.cloudfront.net/key-image/13668e79-77e7-4f4d-804a-2f385246dd1b.jpg)
There are various ways to tween values in Unity, including free third-party libraries such as iTween. Figure 1 shows some manual ways to move an object in Unity.
![Script Script](/uploads/1/2/5/6/125644402/935873975.jpg)
Note that for simplicity, they haven’t been optimized (to do so, I’d hold a reference to the transform in a variable to prevent going from managed code to native code too often). Physics and Collision Detection Physics and collision detection features in 3D are nearly the same as in 2D, except the colliders are shaped differently and the rigidbody component has a few different properties, such as being able to accomplish free rotations or movement in the X, Y and Z axes. In 3D there’s now a mesh collider that wraps the entire shape of a model as a collision-detection zone. This might sound great, and for collisions it’s pretty good, but it’s not good for performance. Ideally, you want to simplify collider shapes and limit the processing power it takes to use them. Have a zombie? No problem, use a capsule collider.
A complex object? Use multiple colliders. Avoid the mesh collider if possible. Unity provides a number of methods to know when a collision happens or a trigger is triggered. The following shows just a basic example.
// Gets the UnityEngine.UI.Text component. Var score = GetComponent ; score.text = 'Score:0'; If I want an image in my UI, I simply click on GameObject UI Image and assign a 2D sprite image to this new component. I can set these values just as with any other game object. I hope you see a pattern by now. To create a simple GUI, create the UI objects via the GameObject UI menu, set the initial values in the Editor and control them later by getting references to those UI components and setting the values, or even animating the values. I built a basic GUI, shown in Figure 10, by creating elements underneath a new Canvas component.
The new Unity 4.6 UI system contains a number of basic object types, such as Panel, Button, Text, Image, Slider, Scrollbar, and Toggle, and it’s incredibly easy to anchor them, scale them, and drag and drop them to create a UI. Figure 10 A UI with an Image and Heads-up Text AI in Your Game It wouldn’t be fair not to mention AI, though I won’t get into creating AI here (even though the building blocks for it are in the earlier code samples for find/move/rotate). But I’ll mention a few options that are available to you. I hesitate to call AI in a game AI, because it’s not so much intelligence as just a very basic action. I showed you how to have a transform rotate toward another object and move that object. That’s the basic AI in many games. Unity has some built-in path-finding capabilities with its NavMesh support, which calculates ahead of time all the paths around objects.
NavMesh works pretty well and is now included in the free edition of Unity, although many choose instead to use the A. Pathfinding Project , which is an algorithm you can either implement yourself, or save yourself the time by purchasing an asset package for it. As of this writing, 2D pathfinding support isn’t built into Unity, only 3D, although A. does have that capability. Behave 2.0 from AngryAnt is a popular AI plug-in for Unity with some really strong features, and there’s also RAIN, a free AI toolkit from, which is also pretty decent and has built-in behaviors for follow, find, Mecanim integration and more. Wrapping Up The 3D world adds an extra layer of complexity over 2D as it deals with full meshes and one more dimension. The Asset Store is absolute key for beginners and advanced alike, and you can really get off to a quick start by using pre-created assets.
When I started developing games, I went crazy finding so many models and textures on the Internet. There are some great asset marketplaces out there, but you’ll quickly find they aren’t all good for games. I once downloaded a small boulder that had near 100,000 vertices in its model!
Look for assets that are mobile-optimized, or check out the vertex/polygon count to ensure you find ones that can work for your games. Otherwise, they can slow your performance down considerably. There are optimization tools you can use on models, including one for Unity called Cruncher.
In the next article, I’ll discuss how to take a game or app from Unity over to the Windows platform. Check out my Channel 9 blog for some videos and links to content to download.