Unity3D development: Creates a fully functional cannon

Source: Internet
Author: User

Create a turret Rotation

As we all know, the turret needs to launch shells from different angles around it. This requires us to set the turret as an object that will rotate. Next we will create a turret together.

Step 1: Add a rotation function to the barrel of the turret.

Add a rotation function to the barrel of the gun so that the gun moves as the mouse moves in the X direction. In the Project file bar, right-click --> Create a JS function, press F2 to change it to "turrenttate", and enter the following code.

Click the turret in the scene panel and find the object corresponding to the upper half of the turret, as shown in figure 11.1:

Fig 11.1

Drag the file to the object.

Step 2: set the camera to follow.

Select the Camera object "Camera" and click --> in the menu bar to add a smooth follow code for the Camera, but no follow object is set here.

Continue to select the camera object, find "Target" in the "Smooth FollowScript)" area of its property panel, and drag the upper half of the gun to it. 11.2:

Fig 11.2

Step 3: run the game and observe the effect.

Click the test button and move the mouse down to see the turret rotation. 11.3:

Fig 11.3

The camera rotates with the turret, while the G.U. I icon and G.U. I text remain unchanged in the upper left corner of the screen, which is very good.

Note: One of the problems that need to be noted here is that the camera action is prone to conflict. Therefore, when setting the camera action, you must be careful to disable the unused camera action. Another point is that when the model is created and exported, you need to set the model's coordinate axis. Otherwise, when the script is executed, the camera cannot be in the same direction as the gun.

Shooting and explosion effects of Shells

After completing the gun rotation, we will continue to make the shell-related effects.

Step 1: create a shell.

On the menu bar, click --> to create a sphere model and use the scenario adjustment tool to adjust it to a size similar to that of the muzzle. 12.1:

Fig 12.1

Step 2: Add a texture to the shell.

Find two metal textures. The first is the basic RGB texture, and the second is the normal display texture, as shown in 12.2:

Fig 12.2

This is to give the object a three-dimensional feeling when giving it a material. You can also take only one texture image for an object. Find the desired texture maps and copy them to the "Assets" resource] folder in the project folder.

Next, select the sphere model, and find the material ball drop-down box in its attribute panel. Change the rendering mode to "Bumped Diffuse", drag and drop the dark texture map to the first texture box, and drag and drop the light texture map to the second texture box. 12.3:

Fig 12.3

So we will get a shell with material effects.

Step 3: Set the shell launch effect.

On the menu bar, choose --> to create a premade Class Object, press F2 to change its name to "Shot", and then drag the sphere model to the premade object, removes the shell model from the scene panel.

However, the sphere model only looks like a shell in appearance, and we have to make it have the same object characteristics as a shell. Click --> on the menu bar to add a rigid body attribute to the shell.

Step 4: Set the launch start point of the shell.

On the menu bar, click --> to create an empty object and place it at the muzzle gate, which serves as the starting point for launching a shell. Press F2 to change it to "FirePoint". In the Hierarchy list bar, drag "FirePoint" to the rotation object in the upper part of the turret so that it can be rotated together with the turret.

Step 5: Create a launch function for the shell.

In the Project file bar, right-click --> Create a JS function, press F2 to change it to "Open Fire", and enter the following code.

After entering the code, drag the code to the launch object "FirePoint" in the Hierarchy list bar. then we will find that the "FirePoint" interface and "Bullet" interface object in the property panel are empty. Therefore, we must drag the FirePoint object and shell object Shot to two corresponding interfaces. 12.4:

Fig 12.4

Step 6: Add spark effect.

Next, press the test button to see the effect. You will find the shells that were fired before they hit the mountain. So the shells we made at this time are just a sphere and won't explode. Next we will continue to make the shell explosion effect.

Enter the keyword "Fireworks" in the search box in the Project file bar and drag it to the scene panel. You will see a dynamic real-time explosion. 12.5:

Fig 12.5

Then, make sure that the object is selected and adjust its attribute values in the property Panel until the desired effect is reached. 12.6:

Fig 12.6

Next, choose --> on the menu bar to create a premade Class Object and press the F2 key to change its name to "BoomPrefab ", drag the explosion spark to the premade object and delete the spark from the scene panel.

Step 7: Create an explosion effect for the shell.

Add a JavaScript code named "Boom". The main function of this Code is to add a collision detection to the shell. If the shells collide with any object, the shells will disappear in the scene, and the prefabricated objects of the explosion sparks will be loaded at the location where the shells crash. The Code is as follows:

After entering the code, save the code and drag it to the "Shot" pre-fabricated shell object. Click the pre-made object "Shot", and find the "Boom" volume display bar that you just dragged to it on its property panel, then drag the BoomPrefab pre-fabricated spark object to its interface "BoomFire". Run the game and check the effect. 12.7:

Fig 12.7

Step 8: Set the explosion effect to disappear.

After a series of settings, you can see the spark effect after the explosion clearly, but it will remain there and will not disappear. This seriously violates the real life scene, so we have to add a time limit for it to disappear code.

Continue to create the JavaScript code and change it to "desBoom". For the specific code, see:

After entering the code, drag and drop the code to the pre-fabricated object BoomPrefab of the explosion spark. Run the program again and we will find that the spark disappears automatically after 2 seconds of explosion.

Step 9: Add sound effects to the explosion effect.

After the screen effect is created, we also need to add a very important element-sound to the game. In fact, this process is very simple. You only need to search for a sound that you think is appropriate on the network, and add the sound file to the "Assets" resource] folder in the project folder. In the Project file bar, drag it to the pre-fabricated object BoomPrefab of the explosion spark, so that the sound file can be generated together with the explosion spark.

If you think the sound needs to be properly adjusted, for example, "Do you want to play when a premade object is generated? "," Whether the sound is played cyclically "and so on. After selecting a premade object, we can find the sound adjustment scroll bar on its property panel and adjust it one by one.

The above is the basic tutorial for this powerful cross-platform 3D Game Development Tool Unity3D. I believe that after reading the exercises, you will surely be able to make excellent 3D games. I hope this tutorial will help you quickly get started with Unity3D. Thank you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.