How to Make an Easy Game on Unity

Ever wanted to learn how to make a game in Unity? Unity is a powerful, cross-platform game engine, and development environment that powers the vast majority of games on the Google Play Store. Through Unity, users gain access to ready-made physics, rendering, controls, and more. This can drastically accelerate the development process. It's thanks to tools like Unity that indie developers are finally able to compete with big studios again.

Also read:The beginner's guide to Android game development: everything you need to know

That could mean you! So, read on to learn how to make a game in Unity.

How to make a basic game in Unity – setting up

This tutorial will assume that you are already familiar with what Unity software is and how it works. If you need more background on that and want advice on how to navigate the admittedly-crowded UI, then check out our introduction to Unity.

For this tutorial, we are going to develop a top-down game that has the player navigate a map to locate keys. This is a great first project in Unity for beginners that will teach some basic concepts.

Maze Game

To that end, the game will be 3D. Start a new project then, and make sure you've selected "3D" under Template. (Unity used to be referred to as Unity 3D, but these days it is just as popular for 2D development.)

Unity tutorial for beginners – building a maze

Now we're going to arrange a few items in our scene. First, we're going to add the ground, which is called a 3D plane in Unity-speak.

To add this to the scene, go to:

GameObject > 3D Object > Plane

This will drop a flat square into your scene. "Scene" is effectively another word for level in Unity, though it can also refer to things like menus. The scene window allows you to view and manipulate the individual elements that are in your game world.

Next, we will add a few cubes. Insert the first one by going to:

GameObject > 3D Object > Cube

This will insert a cube which by default will appear right in the center of the plane. To move elements around, you can select them in the scene, and then choose the arrows icon in the top left. This will then allow you to drag the item on all three axes.

Insert 3D Plane

For our purposes though, we can actually leave this where it is! Now you're going to make more of these boxes. To do that, highlight the first one and click Ctrl + C. Now hit Ctrl + V to paste and a new cube will appear directly over the top of the old one. You'll know this has worked because you'll see another cube now listed in the hierarchy on the left. The hierarchy is essentially a list of everything in your scene, which makes it very easy to find and manipulate individual items. When you go pro at Unity development, you'll need to think about arranging these elements sensibly. It can get a little busy otherwise!

Drag the highlighted cube away from the first cube so that it is directly next to it with no gap. To do this precisely, you need to hold the Ctrl button while dragging. This causes objects to move by a predefined unit, which you'll be able to control in the settings.

Our aim is to make a maze, so drag a few of these around to make something that looks maze-like and challenging. The character will be starting in the top left.

Maze Design

If this is fiddly to do from a fixed angle, hold the Alt key and then drag with the mouse to change your viewing angle. You can also use the mouse wheel to zoom in and out.

Inserting a character

Now you have a level, but in order to know how to make a game in Unity, you also need to create characters that can be controlled. For the sake of simplicity, I'm going with a little ball that can be rolled around the maze!

To create this ball, simply drop a sphere into the scene just as you added the boxes.

This time though, we want to give the shape physics. To do this, you simply need to select it in the hierarchy or the scene view and then view the "inspector" that shows up on the right. This window shows you properties of any selected element and lets you edit them precisely. It also allows you to add "components" to GameObjects, which means you can alter their behavior.

Click "Add Component" and then:

Physics > Rigid Body.

RigidBody is a script that essentially provides ready-made physics to be applied to any object. Our ball will now drop into the scene, ready to be moved around! This is the real power of using a game engine like Unity 3D: it provides built-in features that would otherwise require months of coding and probably a math degree!

This is good advice when learning how to make a game in Unity: don't try and reinvent the wheel. In fact, that goes for coding in general. If someone has already built something that does what you need it to, use that!

I reduced the size of my default sphere to 0.5 by editing the scale on all three axes in the Transform (also found in the inspector).

Rollerball Game

Where you move the ball in the scene is where it will be placed at the start of the game. I want my ball to be level with the ground when the game starts, so an easy little "hack" you can use to accomplish this is to let the game play with the sphere selected so you can see its properties change in the inspector as it falls. Then make a note of where the Y axis ends up once it settles on the ground. That should be your starting point!

Fixing the camera and input

To play this game properly, we want to take a top-down view of the action. To do that, we need to change the angle of the camera and its FoV. So select the camera in the Hierarchy and you should see a small window appear in your scene that shows a preview of what it is seeing.

This also opens up some details in the "Inspector" on the right Where it says "Rotation," we're going to change the X axis to "90."

Now drag the camera up and away from your scene, until you can see the entire map.

Ball in Inspector

But we still need a way to control our game! For that, we're going to need to write our first script.  It's time to learn how to code in Unity!

Don't worry, it's a really simple one and you only need to copy and paste what you see!

Create a new folder in your Assets and call it "Scripts." Now right click anywhere in here and select:

Create > C# Script

Call your new script "TiltControl."

Once this has been created, double click on it to open your default editor (IDE). This will usually be Visual Studio.

Now just delete everything that is there currently and replace it with:

Code

            using System.Collections; using System.Collections.Generic; using UnityEngine;  public class TiltControl : MonoBehaviour {      public Rigidbody rb;          // Start is called before the first frame update     void Start()     {         rb = GetComponent();     }      // Update is called once per frame     void Update()     {             }      void FixedUpdate()     {         Vector3 movement = new Vector3(Input.acceleration.x, 0.0F, Input.acceleration.z);         rb.velocity = movement * 5;     } }          

You don't need to know everything that's happening here, except that the method "fixedUpdate()" runs at fixed intervals. In here, we are calling on the Rigidbody component we added earlier and then adding velocity on three axes based on the accelerometer in the phone. In other words, the player will now be able to move the ball around by tilting the phone!

Also read:Unity certification for developers: Is it worth it?

Now head back into Unity, select the sphere, and drag your TiltControl script into the Inspector at the bottom where it says "Add Component." This now means that the code in your script will affect the GameObject you have attached it to.

And yes: that means you could just as easily cause a whole fleet of balls to move as you tilt the phone!

Keep in mind that this method is sensitive to the starting position of the phone – so you would ideally do something to calculate this prior to the app running if you were going to develop this further.

Before we test the game, you should also tick the box that says "Freeze Position Y" under Constraints. This is important because it will prevent our ball from bouncing out of the maze if it moves too fast!

Making an Android game in Unity for beginners

This is Android Authority, so we want to make Android games!

To do this, select File > Build Settings. Now highlight Android from the list of Platforms, then choose "Switch Platform."

For this to work, you'll need to have the Android SDK and Java JDK already installed and located on your machine. You can request Unity to handle this for you at run-time, otherwise you will need to download them separately and then locate the necessary files. This can also be achieved through the Unity Hub.

Build Settings

You should also click the button that says "Add Open Scenes," which will add the level you've created to the build.

Finally, click "Player Settings" and then scroll down to where it says Default Orientation. You want to set this to "Landscape Right" which will prevent the screen from rotating while your players are having fun!

To build and test the app, you only need to click "Build and Run" while your smartphone is plugged in. Make sure that you have enabled USB debugging in the Developer Options menu.

If all goes to plan, then you should see the game pop up on your device screen after a few minutes of building. Congratulations: your first Android app built in Unity!

#Winning

But it's not really a game until you can win! To add winning conditions, we're going to make one of our blocks into a goal.

Drag and drop a simple blue square PNG into your Project window (you can create a new folder called "Colors" or something if you like). Select one of the squares in your game and then drag and drop that color onto it.

Now we need to create another new script, which I'm calling "WinBlock." This one looks like so:

Code

            using System.Collections; using System.Collections.Generic; using UnityEngine;  public class WinBlock : MonoBehaviour {     // Start is called before the first frame update     void Start()     {              }      // Update is called once per frame     void Update()     {              }      void OnCollisionEnter(Collision collision)     {         Application.Quit();     }  }          

What this is doing, is checking to see if anything bumps into it. All these cubes have "Colliders" by default, which are boundaries that allow Rigidbody to know where obstacles start and end. This is another common feature of video game engines that saves developers a lot of time.

So when something new touches that boundary, the game exits! Seeing as the only thing that can move is our little ball, we can safely assume that this is going to be the culprit.

Of course, simply exiting the game when you win is a little unceremonious. Ideally, you would probably play a sound and then launch the next level (which would mean creating a new scene).

What next?

There is a lot more you would need to do to make this game fun – let alone sellable. We need to add textures, music, graphics, fine-tune the gameplay. If we were building this into a bigger project, we would also need to reconsider how we have arranged the elements in our scene.

Unity Maze Game

Still, as a starting point, I think you'll agree it's pretty impressive what we've managed to accomplish in a very short time. And we've learned some basic lessons along the way.

This was the first game you ever built with Unity!

I hope it won't be your last.

If you're ready to learn more, then I recommend checking out one of our other Unity tutorials for beginners:

We have lots of tutorials to get you started with Android game development in Unity, why not start with our guide to building your first basic Android game in just 7 minutes?


For more developer news, features, and tutorials from Android Authority, don't miss signing up for the monthly newsletter below!

meyersprioned.blogspot.com

Source: https://www.androidauthority.com/how-to-make-a-game-in-unity-1130929/

0 Response to "How to Make an Easy Game on Unity"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel