1. Trang chủ
  2. » Công Nghệ Thông Tin

Các bước học lập trình game unity từ đơn giản đến phức tạp

967 38 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Nội dung

Unity is a comprehensive yet simple suite of tools for developing video games. You can use Unity to not only create video games, but also ARVR experiences, complex simulations, realtime realistic rendering, films, and practical games for training and education. With this book, you will get to grips with creating a full game from the ground up, building it stepbystep and applying your knowledge as you progress. Complete with handson tutorials and projects, this easytofollow guide will teach you how to develop the game using several Unity tools. As you advance, you will learn how to use the Unity engine, create simple scripts using C, integrate graphics, sound, and animations, and manipulate physics to create interesting mechanics for your game. Youll be able to apply all the knowledge that you gain to a realworld game. Later chapters will show you how to code a simple AI agent to challenge the user and use profiling tools to ensure that the code runs efficiently. Finally, youll work with Unitys AR tools to create AR experiences for 3D apps and games. By the end of this Unity book, you will have created a complete game and built a solid foundation in using a wide variety of Unity tools.

Book Description Unity is a comprehensive yet simple suite of tools for developing video games You can use Unity to not only create video games, but also AR/VR experiences, complex simulations, realtime realistic rendering, films, and practical games for training and education With this book, you will get to grips with creating a full game from the ground up, building it step-by-step and applying your knowledge as you progress Complete with hands-on tutorials and projects, this easy-to-follow guide will teach you how to develop the game using several Unity tools As you advance, you will learn how to use the Unity engine, create simple scripts using C#, integrate graphics, sound, and animations, and manipulate physics to create interesting mechanics for your game You'll be able to apply all the knowledge that you gain to a real-world game Later chapters will show you how to code a simple AI agent to challenge the user and use profiling tools to ensure that the code runs efficiently Finally, you'll work with Unity's AR tools to create AR experiences for 3D apps and games By the end of this Unity book, you will have created a complete game and built a solid foundation in using a wide variety of Unity tools What you will learn ● Explore both C# and Visual Scripting tools to customize various aspects of a game, such as physics, gameplay, and the UI ● Program rich shaders and effects using Unity's new Shader Graph and Universal Render Pipeline ● Implement postprocessing to improve graphics quality with full-screen effects ● Create rich particle systems for your Unity games from scratch using VFX Graph and Shuriken ● Add animations to your game using the Animator, Cinemachine, and Timeline ● Use the brand new UI Toolkit package to create user interfaces ● Implement game AI to control character behavior Section – Our First Level In this section, you will learn about the fundamental concepts of Unity, such as scene creation and asset management, to create your first playable prototype game level This section comprises the following chapters: ● Chapter 1, Designing a Game from Scratch ● Chapter 2, Setting Up Unity ● Chapter 3, Working with Scenes and Game Objects ● Chapter 4, Grayboxing with Terrain and ProBuilder ● Chapter 5, Importing and Integrating Assets Chapter 1: Designing a Game from Scratch Welcome to the first chapter of the book! I am sure you are as super excited as I am to start this amazing journey into game development with Unity We will be approaching game development in four parts First, we will be talking about the basics of game development, looking at topics such as how to design your game before you start coding, and then we will prototype a simple first level using Unity Then, we will dive into graphics to explore the look and feel of a good game Later, we will learn how to get everything moving through the use of scripting Finally, we will see how you can finish and publish your game As you go through the chapters, you will apply every concept to a full game project, so you will end the book with a fully functional shooter game In this chapter, we will design our game, Super Shooter This phase is known as pre-production, where we will create a development plan Our game design will include all the functionality we want in our game: the player character, the non-player characters, game assets, animations, and more We will also use screen mock-ups to document our game's design We will look at related concepts regarding the use of Unity for our game along the way We will be discussing which pieces of documentation are necessary for all design work we will be doing throughout this chapter Specifically, we will examine the following concepts in this chapter: ● Game concept ● Game characters ● Gameplay ● The difficulty balance ● Documentation Game concept Why not just start developing our game instead of designing it? This question is spawned from the excitement of developing games, especially with the Unity game engine All games start with an idea That idea is translated into a design, and that design is the basis for development and, eventually, the final game A game's design is like a blueprint for a house You would not consider building a house without a blueprint, and it is an equally bad idea to develop a game without designing it first The reason for this is to save time and frustration For larger projects, time wasted also means unnecessary funds are expended Imagine that you employed a project team of 12 developers, animators, and artists If you shared your game idea, would they have enough information to go on? Would they create a great game, but not the game you had in mind? All we are doing with our game design is documenting as much as we can in the beginning so that the development process is purposeful Without question, you will continually modify your game's design during development, so having a strong base from which to start is critical to your success Our game design will serve as the foundation for the look of our game, what the player's objectives are, what the gameplay will be, supporting user actions, animations, audio, Artificial Intelligence (AI), and victory conditions That is a lot to think about and underscores the importance of translating the game idea into the game design Throughout the book, we will be covering a range of components However, in this section, we will cover those that appear in the following list: ● Game idea ● Input controls ● Winning and losing So, let's look at each component in more detail Game idea The basic concept of our Super Shooter game is that it will be a 3D game featuring a Futuristic Hero Soldier as the player character The character must fight against Enemy Soldiers, who are intent on destroying our Hero's base and anyone that gets in their way, including our Hero Here is an image of what our game will look like: Figure 1.1 – Our hero shooting bullets at enemies Now that we have a general idea of what the game is going to be, let's talk about how the player will control the character Input controls It is important to consider how players will interact with our game Players have an expectation that the industry norms for user controls will be implemented in games, which is why, for our example, the player will control our Hero using the standard set of controls Our default set of user input controls, as shown in the following figure, will consist of the keyboard and mouse: Figure 1.2 – Controls scheme We will configure and program our game so that user input from the keyboard matches the key and action pairings shown in the following table: 10 If you see a Failed to register bundle identifier error, just change the Bundle Identifier setting for another one, always respecting the format (com.XXXX.XXXX), and then click on Try Again until it is solved Once you find one that works, set it in Unity (Bundle Identifier under Player Settings) to avoid needing to change it in every build 11 Hit the Play button at the top-left part of the window and wait for the build to complete You might be prompted to enter your password a couple of times in the process, so please so 12 When the build completes, remember to unlock the device A prompt will ask you to that Note that the process won't continue unless you unlock the phone 13 After completion, you may see an error saying that the app couldn't be launched but that it was installed anymore If you try to open it, it will say you need to trust the developer of the app, which you can by going to the settings of your device 14 From there, go to General | Profile & Device Management and select the first developer in the list 15 Click the blue Trust … button and then Trust 16 Open the app again 17 Please remember to try another image if you're having trouble detecting the image where we instantiate the player (the Unity logo, in my case) This might vary a lot, depending on your device's capabilities In this section, we discussed how to build a Unity project that can run on iOS and Android, thus allowing us to create mobile apps – AR mobile apps, specifically Like any build, there are methods we can follow to profile and debug, as we saw when we looked at PC builds, but we are not going to discuss that here Now that we have created our first test project, we will convert it into a real game by adding some mechanics to it Creating a simple AR game As we discussed previously, the idea is to create a simple game where we can move our player while moving a real-life image, and also put in some Enemy Spawners by just tapping where we want them to be, such as a wall, the floor, a table, and so on Our player will automatically shoot at the nearest Enemy, and the enemies will shoot directly at the player, so our only task will be to move the Player so that they avoid bullets We are going to implement these game mechanics using scripts very similar to the ones we used in this book's main project In this section, we will develop the following AR game features: ● Spawning the Player and Enemies ● Coding the Player and Enemy behavior First, we are going to discuss how to make our Player and Enemies appear on the app, specifically in real-world positions, and then we will make them move and shoot each other to create the specified gameplay mechanics Let's start with spawning Spawning the Player and Enemies Let's start with the Player, since that's the easiest one to deal with: we will create a prefab with the graphics we want the player to have (in my case, just a cube), a Rigidbody with Is Kinematic checked (the Player will move), and an AR Tracked Image script We will set that prefab as Tracked Image Prefab of the AR Tracked Image Manager component in the AR Session Origin object This will put the Player on the tracked image Remember to set the size of the Player in terms of real-life sizes In my case, I scaled the Player to (0.05, 0.05, 0.05) Since the original cube is meter in size, this means that my player will be 5x5x5 centimeters Your Player prefab should look as follows: Figure 23.36 – The starting Player prefab The enemies will require a little bit more work, as shown here: Create a prefab called Spawner with the graphic you want your Spawner to have (in my case, a cylinder) and its real-life size Add a custom script that spawns a prefab every few seconds, such as the one shown in the following screenshot You will notice the usage of Physics.IgnoreCollision to prevent the Spawner from colliding with the Spawner object, getting the colliders of both objects and passing them to the function You can also use the Layer Collision Matrix to prevent collisions, just like we did with this book's main project if you prefer to: Figure 23.37 – Spawner script Create an Enemy prefab with the desired graphic (a Capsule, in my case) and a Rigidbody component with the Is Kinematic checkbox checked This way, the Enemy will move but not with physics Remember to consider the real-life size of the Enemy Set the Prefab property of the Spawner so that it spawns the Enemy at your desired frequency: Figure 23.38 – Configuring the Spawner Add a new SpawnerPlacer custom script to the AR Session Origin object that instantiates a prefab in the place the player tapped using the AR Raycast system, as shown in the following screenshot: Figure 23.39 – Placing the Spawners Set the prefab of SpawnerPlacer so that it spawns the Spawner prefab we created earlier And that's all for the first part If you test the game now, you will be able to tap on the detected planes in the app and see how the Spawner starts creating enemies You can also look at the target image and see our Cube Player appear Now that we have the objects in the scene, let's make them something more interesting, starting with the Enemies Coding the Player and Enemy behavior The Enemy must move toward the player in order to shoot at them, so it will need to have access to the player position Since the Enemy is instantiated, we cannot drag the Player reference to the prefab However, the Player has also been instantiated, so we can add a PlayerManager script to the player that uses the Singleton pattern (as we did with managers) To that, follow these steps: Create a PlayerManager script similar to the one shown in the following screenshot and add it to the Player: Figure 23.40 – Creating the PlayerManager script Now that the Enemy has a reference to the player, let's make them look at the player by adding a LookAtPlayer script, as shown here: Figure 23.41 – Creating the LookAtPlayer script Also, add a simple MoveForward script like the one shown in the following screenshot to make the Enemy not only look at the player but also move toward them Since the LookAtPlayer script is making the Enemy face the Player, this script moving along the z axis is just enough: Figure 23.42 – Creating the MoveForward script Now, we will take care of the Player movement Remember that our player is controlled through moving the image, so here, we are actually referring to the rotation, since the player will need to automatically look and shoot at the nearest Enemy To this, follow these steps: Create an Enemy script and add it to the Enemy prefab Create an EnemyManager script like the one shown in the following screenshot and add it to an empty EnemyManager object in the scene: Figure 23.43 – Creating the EnemyManager script In the Enemy script, make sure to register the object in the all list of EnemyManager, as we did previously with WavesManager in this book's main project: Figure 23.44 – Creating the Enemy script Create a LookAtNearestEnemy script like the one shown in the following screenshot and add it to the Player prefab to make it look at the nearest Enemy: Figure 23.45 – Looking at the nearest Enemy Now that our objects are rotating and moving as expected, the only thing missing is shooting and damaging Create a Life script like the one shown in the following screenshot and add it to both the Player and Enemy components Remember to set a value for the field for the amount of life You will see this version of Life instead of needing to check whether the life amount has reached zero every frame We have created a Damage function to check that damage is dealt (the Damage function is executed), but the other version of this book's project also works: Figure 23.46 – Creating a Life component Create a Bullet prefab with the desired graphics, the collider with the Is Trigger checkbox on the collider checked, a Rigidbody component with Is Kinematic checked (a Kinematic Trigger Collider), and the proper real-life size Add the MoveForward script to the Bullet prefab to make it move Remember to set the speed Add a Spawner script to both the Player and Enemy components and set the Bullet prefab as the prefab to spawn, as well as the desired spawn frequency Add a Damager script like the one shown in the following screenshot to the Bullet prefab to make bullets inflict damage on the objects they touch Remember to set the damage: Figure 23.47 – Creating a Damager script – part 10 Add an AutoDestroy script like the one shown in the following screenshot to the Bullet prefab to make it despawn after a while Remember to set the destroy time: Figure 23.48 – Creating a Damager script – part And that's all! As you can see, we basically created a new game using almost the same scripts we used in the main game, mostly because we designed them to be generic (and the game genres are almost the same) Of course, this project can be improved a lot, but we have a nice base project to create amazing AR apps Summary In this chapter, we introduced the AR Foundation Unity framework and explored how to set it up and how to implement several tracking features so that we can position virtual objects on top of real-life objects We also discussed how to build our project so that it can run on both iOS and Android platforms, which is the only way we can test our AR apps at the time of writing Finally, we created a simple AR game based on the game we created in the main project but modified it so that it's suitable for use in AR scenarios With this new knowledge, you will be able to start your path as an AR app developer, creating apps that augment real objects with virtual objects by detecting the positions of the real objects This can be applied to games, training apps, and simulations You may even be able to find new fields of usage, so take advantage of this new technology and its new possibilities! Well, this is the end of this journey into Unity 2021; I'm really glad you reached this point in the book I hope this knowledge will help you to improve or start your Game Development career with one of the most versatile and powerful tools on the market, Unity Hope to see your creations someday! See you on the road! ... ● Unity technical requirements ● Unity installs ● Installing Unity with Unity Hub Let's start by discussing what we need to run Unity on our computers Unity Technical Requirements To run Unity. .. engine such as Unity? ● Installing Unity ● Creating projects Let's start by talking about why you should choose Unity to start your game development career Why use a game engine such as Unity? When... install Unity Let's learn more about it Installing Unity with Unity Hub Unity Hub is a small piece of software you install before installing Unity It centralizes the management of all your Unity

Ngày đăng: 04/01/2022, 09:38

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w