First, the preparation of knowledge-the object of "life" and "death"
(1) How do I create objects in a game script instead of creating them from the beginning? static method for using Gameobject:createprimitive ()
Based on the "Where to play" example in the blog post above, write the following code in the Addforce script:
View Code
In the Createcube method, you use the Gameobject.createprimitive method to create a game object instance of the cube type, set the coordinates it appears in, and add the Rigidbody component to it. Here you can see the AddComponent method, whose parameters are a generic, which means that the components we see in the properties panel, such as rigid bodies, audio sources, and even scripts, can be dynamically added by means of the Addcomponet method. Now look at the effect of creating a cube object in the game by clicking the left mouse button:
(2) Careful readers will find that when we create countless cube objects, the computer's memory usage increases gradually. Wit you will surely think of destroying the created game object in time, freeing up memory resources. Don't worry, Unity3d provides us with a very convenient way to do this:Destroy (). This function provides two overloads: the first one you can pass the ID of a game object directly (for example, we created a plane in the previous example, its ID is also plane); the second one you can pass two parameters, one is the ID of the game object just mentioned, The other is the number of seconds to delay destruction (that is, it can disappear from the screen after the specified number of seconds, destroying it from memory);
Let's re-modify the Addforce script to the following code:
View Code
Here we encapsulate the code that destroys the game object as a method: Destroygameobject (), It first finds the game object with the specified ID through the Gameobject.find method, and then calls the method of destroying the game object destroy destroys it, using the second overload, which passes a 2-second delay time. That is, when we click the left mouse button for 2 seconds, the plane object will be destroyed, our sphere sphere object will fall into the abyss! Now let's see how it works:
While previewing the effect, you can look at the list of objects in the left hierarchy, and when did plane disappear?
Well, the preparation of knowledge to this end, and now we really start crazysphere(abbreviation:CS, sounds tall on it, click!) -" Crazy hit Box " Game development tour!
Second, crazy hit box-crazysphere realization of the road
Since our "CS" is to hit the box, wooden box how to do it! Now, let's create some boxes as the objects we hit.
First of all, given that there are 16 of chests to initialize, we need to create these boxes in the program and put them in the plane to form the box walls and let our balls hit.
(1) Create the following objects in hierarchy: A direction light, a plane; Set the position of plane to (0,1,-6) so that it looks clearer;
(2) Preparatory work:
① Create the following folder in assets: A images folder for mapping files, a musics folder for background music and sound effects MP3, a scripts folder to store C # script files, as shown in:
② to images inside to import some stickers, I found here on the internet a few pictures of Angry Birds, used to give plane, sphere and cube objects to render material maps, so that the game objects look good. (For specific documents please download the source code in the attachment, find assets/images/)
③ into the musics inside a background music and a sound file, background music as the game background music default and loop playback, sound music as a small ball impact box sound effects played when the ball is emitted. Here background music I choose Sax Classic-Home, is not very literary? Sound, I just went online to find the sound of a cannonball launch. (For specific documents please download the source code in the attachment, find assets/musics/)
Then select the main Camera in hierarchy, select Component->audio->audio Source in the main menu bar, and Audio in the properties Select the imported background music (here is gohome-sax) in the source block and tick the play on awake (default play) and loop (loop) check box, we will play the Gohome-sax.mp3 file at the beginning of the game.
Finally, follow the above steps to add audio Source to plane, and select it as bomb as the initial sound.
④ Create two C # Script in scripts, one named Initsceneand the other named Autodestroy. The Initscene script is used to initialize the game scene, which is the 4*4 box matrix. The Autodestroy script is used to destroy the game object beyond the visual range of the main camera, which is automatically destroyed when our ball or hit box exceeds the plane's ground range or falls.
(3) First, write the Autodestroy script, using the method we learned in the preliminary knowledge to automatically destroy objects. This Autodestroy script is meant to be attached to a game object that needs to be destroyed automatically, so this script is appended to the automatically created object in the main script of the init scene (using the generic method provided by addcomponent).
View Code
The Ps:onbecameinvisible () method is a self-contained method in Unity3d that is triggered when a specific game object is not visible on the game screen. You can understand that it is similar to the Application_End () event in the global file in ASP. WebForm. Here, we destroy the specific game object when the game object is not visible. Note that the method parameter that is destroyed here is this. Gameobject, not this!.
(4) Now let's write the Initscene script, this is the point! When you are finished writing, attach the script to the main camera object!
View Code
Now we hit analyze This script code:
The ①createcubes () method defines the implementation process for initializing 4*4 chests, once each loop creates a cube of type cube by Createprimitive, and then sets the position coordinates for each cube, increases the rigid body components, Increase the script to automatically destroy and render the map for it.
In the update () method, the ② controls the ball's impact on the mouse-specified coordinates: When the user clicks the left mouse button, a sphere ball is created, still setting coordinates, adding rigid body components, rendering the map, and increasing the script to automatically destroy it. It is important to note that the coordinates of the ball should be the position of the camera, because the ball is flying out of the camera. Then, through the screen coordinates to the world coordinates to obtain the target vector, and then through the target vector-the camera vector = Direction vector (here is related to vector subtraction, do not understand the reader can look at the second part of this article 3D model basis, or to review the geometric meaning of high school vector subtraction). Finally, for the ball to add a mouse click on the direction of how much force, it will go in that direction (here is "Fly"). To highlight the effect, it also adds a sound effect to the ball and plays it when it is emitted.
(5) Here, a basic crazysphere can be achieved, now we look at the effect: you can let the ball in accordance with the coordinates we set to launch, the launch will also have the sound of shells, and background music has been playing in the loop, a demo is almost finished, is not very soon!
(6) But do you think our game background is too monotonous, no matter, unity3d for us to provide the skyboxes-sky box, let our background one second into a brilliant blue sky! (For more information about Sky boxes, see the References to the Sky box, which is no longer described here) here we add a sunny sky box to the scene:
① Right-click at assets, select Import package->skyboxes, select Sunny1 mat, SUNNY1-related TIF resource in the pop-up selection box. Be careful not to bring all the sky boxes in, so the file will be very big!
② Click on the main menu bar Edit->render Settings, find Skybox Material in the property bar on the right:
The selection button on the right side of the machine, in the pop-up selection box can see we just imported Sunny1 this sky box, double machine select it, so we let the game background one second into the sun bright blue sky, is not relaxed and happy Ah!
(6) Now, let's take a look at the game effect: Is it a blue sky? In this way, our Crazysphere v1.0 is developed!
Iii. Summary
Through a few days of unity3d study, we learned the basic knowledge of Unity3d, 3D Model Foundation, Physics Engine Foundation, and integrated this knowledge to do a small game: crazysphere-Crazy hit the box game, but also to achieve the background music, sound effects play, Join the Sky Box to make the game background look good. Of course, this series as a preliminary study, it is impossible to learn in depth, but at least we can get started, we can initially appreciate the Unity3d's strong charm and our. NET Programmer's learning advantage.
Unconsciously, has written four about Unity3d's study notes, and this is my first series of blog posts, for my career has important significance, once again thanks to me to encourage the park friends, let me as a newcomer feel honored. In addition, this article is based on the two public lessons of Unity3d podcast, which was compiled, heartfelt thanks to the Yang Zhengko and the sharing of the wisdom of the biography, as well as the encouragement of Lao Yang. Soon to school, but also back to Chengdu, the bitter force of postgraduate career still have to continue, good want to graduate early Ah! Was sent outside the school internship, the teacher (laboratory guidance teacher, not my mentor, my mentor is pretty good) also not allowed to pay, every day also dry Red. However, I would like to wish you all the Garden Friends code year auspicious, more than 2014 yards healthier !
Tomorrow is my grandmother 70-year-old birthday, here also wish her happy birthday, good health!
At the end of this article, we will enclose the demo file of this mini-game case for download, which contains all the materials used in this session. In addition, the demo also uses a GUI to customize the mouse display, replacing the mouse display with a map aiming at the star, as shown in:
References and literature
(1) Preach Wisdom Podcast Unity3d Open class: Http://net.itcast.cn/subject/Unity3D/index.html
(2) XieXuan2007, "Unity3d Sky Box": http://blog.csdn.net/xiexuan2007/article/details/18401075
(3) Ding small not, "Unity3d development Similar bowling game": http://blog.csdn.net/dingxiaowei2013/article/details/9734935
Attachment
Demo File Download: HTTP://PAN.BAIDU.COM/S/1EQZKRDW
Zhou Xurong
Source: http://www.cnblogs.com/edisonchou/
The copyright of this article is owned by the author and the blog Park, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to give the original link.
On the development of Unity3d game