We also do not have links that lead to sites DMCA copyright infringement. If You feel that this book is belong to you and you want to unpublish it, Please Contact us. Unity Games by Tutorials. Download e-Book. Posted on. Page Count. Components: Customize your GameObjects by the way of components. Physics: Unleash the power of the built-in physics engine. Pathfinding: Learn about the pathfinding system to give direction to your monsters. User Interface: Provide custom user interfaces for players to use in your game.
The intensity slider controls the brightness of the light, and the range controls how quickly it fades out. The guidelines in the scene view show you the maximum range of the illumination. Play with both settings to achieve the desired effect. You can also adjust the color of the light, the pattern cookie displayed on the surface the light is pointed at, and what kind of flare appears onscreen when looking directly at the light.
The cookie can be used to fake more realistic light patterns, create dramatic false shadows, and simulate projectors. The three main kinds of light are spot , point , and directional. Spot lights have a location in 3D space and project light only in one direction in a cone of variable angle. These are good for flashlights, searchlights, and, in general, give you more precise control of lighting. Spot lights can cast shadows. Point lights have a location in 3D space, and cast light evenly in all directions.
Point lights do not cast shadows. Directional lights , finally, are used to simulate sunlight: they project light in a direction as though from infinitely far away.
Directional lights affect every object in the scene, and can produce shadows. A Particle system is a GameObject that generates and controls hundreds or thousands of particles simultaneously. Particles are small, optimized 2D objects displayed in 3D space. Particle systems use simplified rendering and physics, but can display thousands of entities in real time without stuttering, making them ideal for smoke, fire, rain, sparks, magic effects, and more.
You can change the size, speed, direction, rotation, color, and texture of each particle, and set most of those parameters to change over time as well. First, we need two paddles, and a ball. The whole game will need to be dramatically lit.
That breaks down into a ball object a sphere , a spawner , two paddle props with particle emitters attached, a 3D-text entity , and a spot light. Scale it appropriately, duplicate it , and put a sphere between the paddles for the ball.
Then, create a 3DText object and scale and position it correctly, changing the font size attribute to get a less pixelated image. Next, create two particle systems , pick the characteristics you want, and attach them to the paddles.
Before we finish, we need to create two additional cubes to be bumpers, to prevent the ball from bouncing out of the game area. We can make them invisible by unchecking the mesh renderer in the inspector tab.
If you hit play, you can now see the basic elements of our game laid out. Once you have a script attached to an object, you can revise it by double clicking on it in the inspector. This opens MonoDevelop, the default development environment for Unity.
In essence, Monodevelop is a text editor with features specifically optimized toward programming. Keywords and comments are highlighted in blue and green , and numerical values and strings appear in red. You can build your scripts from inside the editor, to check for syntax errors, like so:. You can then call methods or set variables for each of these elements to enact the changes you want. If you want a script on an object to affect the properties of a different object, you can create an empty GameObject variable in your script, and use the inspector to assign it to another object in the scene.
A list of the elements an object might have is as follows taken from the inspector view of one of our paddles in the above example :. Each of these aspects of the object can be influenced from within a script. The transform functions for a GameObject in Unity control the physical parameters of that object: its scale , its position , and its orientation.
You can access them from within a script like this:. In the above examples, the named variables are of the types specified in the names. You can access the X , Y , and Z components of each for example, transform.
However, to avoid gimbal lock, rotations are handled as Quaternions four-component vectors. Because hand-manipulating quaternions is unintuitive, you can manipulate rotations using Eulerian angles by using the Quaternion. Euler method like so:. Slerp takes in three arguments — the current state, the final state, and the speed of change, and smoothly interpolates between them at the given speed. The syntax looks like this:. The renderer functions in Unity allow you to control the way the surfaces of props are rendered on-screen.
You can reassign the texture, change the color, and change the shader and visibility of the object. Most of these have pretty clear functions. The first example makes the object in question invisible: a useful trick in a number of situations. The second example assigns a new RGB color namely, green to the object in question. The third assigns the main diffuse texture to a new Texture variable. This allows you to assign the physical properties of objects and let the details of their simulation be handled for you.
All physics props require colliders. However, the actual simulation itself is handled by a rigidbody , which can be added in the inspector view. Rigidbodies can be kinematic or nonkinematic. Kinematic physics props collide with and effect nonkinematic physics props around them, but are unaffected by collision themselves. Static kinematic props are the proverbial immoveable objects, and moving kinematic objects are the proverbial unstoppable force for the record, when they collide, they simply pass through each other.
These are all pretty self-explanatory. The only thing to note here is the use of transform. The transform. It can be multiplied by a float to create more force on the object. You can also reference transform. First, at least one of the objects in the collision needs a non-kinematic rigidbody attached to it. Both objects must have correct colliders, set to be non-triggers.
The total speed of both objects must be low enough that they actually collide, instead of simply skipping through one another. The method will look like this:. This method will automatically run during the first frame that another object touches your object. The collision entity other is a reference to the object that you hit. You can, for example, reference its gameobject , rigidbody , and transform characteristics to manipulate it in various ways.
In raycasting, an infinitely thin line a ray is cast through the world from some origin, along some vector, and, when it hits something, the position and other details of the first collision are returned. The code for a raycast looks like this:. This casts a ray from the position of the current object along -Vector3. Once your ray has hit something, you can access hit.
GameObject to manipulate the object you hit. This is done by using Time. Unity supports two kinds of sounds: 2D and 3D sounds. In order to change whether or not a sound is 3D, select it in the project view, switch to the inspector view and select the appropriate option from the dropdown menu, then press the reimport button. You can use myAudioSource. Pause and myAudioSource. Play to control those sound files.
You can adjust the falloff behaviors, volume, and doppler shifting of the sounds under the inspector tab for the audiosource. There are a lot of different kinds of input you can read in, and almost all of them are accessible through the Input and KeyCode objects. Some sample input statements which have a values evaluated every frame are below. The functions of these lines is mostly self explanatory. Using these three kinds of input reference, you can reconstruct the control schemes of most modern 3D computer games.
As the good doctor says, bangups and hangups can happen to you. If there are outright syntax errors with your C , the game will generally refuse to run when you hit play, and some fairly useful error messages are provided if you build the scripts from within the editor.
See below:. These bugs are typically not the most difficult to fix. What can be more problematic are subtle semantic errors, in which you have successfully written a file full of valid C — just not one that does what you thought it would.
The first is to pause the execution of the game, and check the console. Even if there are no errors, warnings can still help to give some clues as to what might be going wrong. You can use Debug. Log String to print the contents of a string to the console when the program execution hits that line. A good first step would be to add a non-kinematic rigidbody to the ball, two kinematic rigidbodies to the paddles, disable gravity for all of them, and assign an appropriate physic material from the standard assets bounce with bounce combine set to multiply.
Below, you can view the script for the ball with explanatory comments. The ball needs to accomplish some basic goals: it should bounce in a complicated pattern, always maintaining movement on both axes, and it should accelerate at a challenging but not impossible pace in the horizontal direction.
Next, we need to script our paddle, which you can view below. The paddle needs to move up and down in response to key presses but not outside certain bounds. It also needs to trigger the particle system when it collides with something. Slerp for maximum simplicity. Finally, we need a script to update the scoreboard and reset the ball when it goes out of bounds.
0コメント