[Unity3d entry] entry-level game project "Tank Sniper" updated

Source: Internet
Author: User

[Unity3d entry] entry-level game project "Tank Sniper" updated

In the previous article, I shared a basic unity3d project "Tank Sniper" that has not yet been fully written ".

This article introduces the latest version of "Tank Sniper.

If you need source code and released Windows and Web applications, you can leave your mailbox with a thumbs up ~

Experience in adjusting Particle System Parameters

As you can see, I have adjusted the particle effect of the missile tail flame. One of the lessons learned here is that analyzing and imitating physical and chemical phenomena in real life can make realistic results. A particle is a point in the flame. We all know that the flame of a candle is divided into three layers. Match a particle from birth to extinction, from center to edge location, from white to yellow to gold (color I don't care, after all, missiles and candles are not exactly the same). Setting the parameters of the Particle System Based on this knowledge can achieve better results.


Explosive Effect of using third-party Detonator

The missile explosion effect updated in this article uses the example prefab provided by the detonator, which only slightly adjusts the size and explosion time. The detonator is easy to use and has excellent results. The example scenario detonator-testwall and PDF instructions in the package are clear at a glance. Note that when importing the detonator package, you must select the "Ignore" button in the "normalmap Settings" window. Otherwise, you will see the square edge of the explosion smoke, the explosion effect is false.

By the way, you must add the detonator-testwall In the example scenario to the release list. Otherwise, the explosive effect cannot be displayed. The reason is unknown.


The missile kills all tanks within a certain radius.

In the previous article, missiles can only cause damage to hit tanks. Using the physics. overlapsphere () method in this article, missiles can cause damage to all tanks within a certain radius and are more authentic.

1 public float explosionradius = 10; 2 public float maxpower = 100; 3 public float maxforce = 10000; 4 void oncollisionenter (collision) {5 var point = collision. contacts [0]. point; // obtain the position of the missile hitting (explosion) 6 collider [] colcyclers = physics. overlapsphere (point, explosionradius); // obtain all collision bodies in the missile explosion range 7 destroy (this. gameobject); // The Missile no longer exists 8 foreach (collider hit in colcyclers) {9 If (hit = NULL) {continu E;} 10 if (hit. Rigidbody = NULL) {continue;} 11 if (hit. gameobject. Tag! = "Tank") {continue;} 12 var tank = hit. gameobject. getcomponent <tankmove> (); 13 if (tank! = NULL) {// The collision body is a tank 14 tank. damage (maxpower); // The tank is killed (it is temporarily considered that the lethal force within the killing radius is the same, and the computation is simplified) 15 if (tank. HP <= 0) {// The tank crashed 16 hit. rigidbody. addexplosionforce (maxforce, point, explosionradius); // The tank is washed away by the explosion shock wave 17} 18} 19} 20 explosioneffecthelper. instance. explode (explosioneffecthelper. explosioneffect. missileexplosion, point); // detonator explosion effect 21 soundeffecthelper. instance. makeexplosionsound (); // explosive sound 22}

PS: When I know "physics. overlapsphere () "before this method, I created a sphere after the missile explosion to represent the spherical shock wave of the missile explosion, this sphere is used to compress the tank with the oncollisionenter event of the surrounding tank during the expansion process. In this way, although there are peripheral killing capabilities, the power of the calculation of the shock wave is a bit cumbersome; and the instantaneous killing of the explosion becomes the killing of several frames after the sphere expands, which is not true.


Tank hitting tank Problems

In the previous article, the fast-moving tank behind the tank hits the slow-moving tank, and the two may fly. This article solves this problem. The method is as follows:

In the tank prefab, place a cube on the left and right sides to enclose it (and deselect the mesh Renderer of the cube and select is trigger) to create a "wall ". Set ontriggerenter () and other methods in the cube (named front) in front of the tank Prefab.

1 tankmove tankmovescript; 2 void awake () {3 this. tankmovescript = This. getcomponentinparent <tankmove> (); 4} 5 6 void ontriggerenter (collider other) 7 {If (Other. gameobject. tag = "back") // other is the "wall" of a previous tank 8 {9 this. tankmovescript. setforward (false); // hit the "wall" of the previous tank, so do not continue to walk for now 10} 11} 12 13 void ontriggerexit (collider other) 14 {15 if (Other. gameobject. tag = "back") // other is the "wall" of the previous tank 16 {17 this. tankmovescript. setforward (true); // if the tank in front of him walks away, I will continue to go 18} 19}

The ontriggerenter () and other methods are also set in the cube (named back) behind the tank Prefab.

1 private gameobject other; 2 void ontriggerenter (collider other) 3 {If (Other. gameobject. tag = "Front") // other is the "wall" 4 {5 this. other = Other. gameobject; // I blocked a tank behind 6} 7} 8 9 void ontriggerexit (collider other) 10 {11 if (Other. gameobject. tag = "Front") 12 {13 this. other = NULL; // I no longer block the back tank 14} 15} 16 17 void ondestroy () {18 var o = Other; 19 if (o! = NULL) {// if I get down when I block the next tank, I have to notify 20 O. getcomponentinparent <tankmove> (). setforward (true); 21} 22}


Show some information

For example, I want to display the number of tanks I have killed on the screen.

Create a script named "displaykilledtankcount. cs" and write the following code.

1     public int killedTank = 0;2     void OnGUI() {3         GUI.Label(new Rect(10, 10, 200, 20), string.Format("Killed tank: {0}", killedTank));4     }

Add this script as a component to main camera.

Add two lines of code in the "damage ()" method of "tankmove. cs:

1 public float HP = 100; 2 Public Void Damage (float damagecount) {3 HP-= damagecount; 4 If (HP <= 0) {5 this. gameobject. rigidbody. freezerotation = false; // if it fails, it can be impacted by a missile to any angle. 6 var display = camera. main. getcomponent <displaykilledtankcount> (); 7 display. killedtank + = 1; // Add 1 8 destroy (gameobject, 20) to the kill tank; 9} 10}


Tank doll Effect

The performance of the doll is that when a person dies, it will present various styles. It will not be as straight as it was before, as it is like a stick ~

If the tank crashes, it should not be immediately destroy (). It is too abrupt. What I want to do is that when a tank fails, it will be hit by a missile shock wave, landed, and then fell into the ground and disappeared before destroy ().

In fact, the code above already shows how to achieve this effect.

First, when a missile kills a tank, it is set to 20 seconds (experience value) in the damage () method and then destroy ().

Then, add the field "deadtime" to tankmove. CS to record how long it took for the tank to crash. After a tank crashes for 10 seconds (experience value), we can make it underground.

1 private float deadtime = 0; 2 Private bool sinking = false; 3 // call each frame to update the game scene and status (updates related to the physical status should be placed in fixedupdate) 4 void Update () {5 If ((! Sinking) & (HP <= 0) {6 deadtime + = time. deltatime; 7 if (deadtime> = 10) {8 This. gameobject. collider. istrigger = true; // this way, the physical engine will not take effect for the tank. (Of course, The ontriggerxxx () event will still be triggered) 9 sinking = true; 10} 11} 12}

Finally, the tank should not be powered after it is mounted.

1 // each fixed physical time interval (physics time step) is called once for physical state Update 2 void fixedupdate () {3 if (HP <= 0) {return ;} // if you get down, stop yourself. 4 If (this. forward) {5 this. rigidbody. velocity = new vector3 (0, Rigidbody. velocity. y, zspeed); 6} 7 else {8 This. rigidbody. velocity = new vector3 (0, Rigidbody. velocity. y, 0); 9} 10}


If you need source code and released Windows and Web applications, you can leave your mailbox with a thumbs up ~

Thank you for your understanding and support ~

[Unity3d entry] entry-level game project "Tank Sniper" updated

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.