[Learn Unity3D with me] As a 2D director of the 90 tank war and a prop system, the unity3d tank war

Source: Internet
Author: User

[Learn Unity3D with me] As a 2D director of the 90 tank war and a prop system, the unity3d tank war

I am still used to using a director class to control the game process, such as the start and end of the game and the generation of AI in the game, the game maps are all placed in such a director class. Then I put this director class in MainCamera as its component.

First, in Start, you need to initialize some game data, such as reading maps and initializing AI.

Void Start () {// initialize data records = 0; m_iAITankCount = 1; m_iKillAICount = 0; m_iDirection = 0; m_iMyLife = 3; m_bIsLose = false; m_vInstPosition = new Vector3 (0, 4.2f, 1); // read the map m_sXmlPath = Application. dataPath + "/Map/map_1.xml"; // read map from xml ReadFromXml (); m_gMyTank.GetComponent <CMyTank> (). m_fMoveSpeed = m_iMyMoveSpeed; // create an AI while (m_iAICurTankCount <m_iAITankCount) {StartCoroutine (CreateAI (1.0f);} // an InvokeRepeating ("CreateProp ", 15, 30 );}

The focus here is the StartCoroutine function, which is used to start a collaboration program. What is a collaboration program? In my understanding, the collaboration program is the function that is executed immediately, the thread will not be blocked. However, if this function contains the yield keyword, the functions under yield will continue execution after the yield statement is executed. The following example shows how to implement the CreateAI function:

IEnumerator CreateAI (float waitTime) {Vector3 temp_position = m_vInstPosition + RandDirection (); GameObject CreateAIAnimation = Instantiate (m_CreateAIAnimation, temp_position, Quaternion. identity) as GameObject; Destroy (CreateAIAnimation, waitTime); ++ m_iAICurTankCount; yield return new WaitForSeconds (waitTime); <span style = "white-space: pre "> </span> // start: After the waitTime is delayed, GameObject temp_AI = Instantiate (m_gAITank, temp_position, Quaternion. identity) as GameObject; temp_AI.GetComponent <CAITank> (). m_fMoveSpeed = m_iAIMoveSpeed; // end: This statement will be executed after waitTime seconds}
The initial position of AI generation is pseudo-random. AI is generated in turn in three places at the top of the map. The implementation of the RandDirection function is as follows:
// Generate a random direction Vector3 RandDirection () {m_iDirection = ++ m_iDirection % 3; Vector3 vec = Vector3.left * 0; switch (m_iDirection) {case 0: vec = Vector3.right * 4.2f; break; case 1: vec = Vector3.left * 4.2f; break; case 2: vec = Vector3.left * 0; break;} return vec ;}
Then, create the item. The method for creating the item is the same as that for the previous bullet. The first item is created 15 seconds after the start, and the item is created every 30 seconds after the start. The specific implementation of CreateProp is as follows:

    void CreateProp()    {        GameObject Temp = Instantiate(m_gProp, RandPosition(), Quaternion.identity) as GameObject;        Destroy(Temp, 20.0f);    }
The function is to randomly generate an item. The position of the item is random on the map. The item is automatically eliminated 20 seconds after the item is generated. The random function RandPosition on the map is implemented as follows:

    Vector3 RandPosition()    {        int temp_x = UnityEngine.Random.Range(-7, 7);        int temp_y = UnityEngine.Random.Range(-7, 7);        return new Vector3(temp_x * 0.6f, temp_y * 0.6f, -2.0f);    }

The size of the entire map is 8.4x8.4, and each grid is 0.6. The center of the map is the origin of the coordinates. Why do I set the Z axis to-2.0f, this ensures that the item will always float on the map (the Z axis of the grass is-1.0f ).

I only made one item, that is, a bomb. After the item is obtained, all AI will explode. I made a separate script for the implementation of this item. The specific implementation is as follows:

Using UnityEngine; using System. collections; public class CProp: MonoBehaviour {void Start () {} void Update () {} void OnCollisionEnter2D (Collision2D coll) {Debug. log ("OnCollisionEnter2D:" + coll. gameObject. name); // if (coll. gameObject. name = "MyTank") {Destroy (this. gameObject); GameObject [] ais = GameObject. findGameObjectsWithTag ("AITank"); foreach (GameObject ai in ais) {Destroy (ai); Camera. main. getComponent <CCamera> (). boom ();}}}}

You can add attributes to the item later.


I have read the map as mentioned in my previous blog.


Project source code is here: [learn Unity3D with me] To build a 2D 90 tank war of various walls <attached project source code>


Hi, I saw you fight against the UNITY3D tank and asked you how to add AI.

You can add several empty objects, place them on the map, and move the computer back and forth between the points.

How to Use unity3d + free extension tools to develop 2D games?

This section describes how to use unity3d + free extension tools to develop 2D games.
This section is followed by the previous section.
Create an animated brick
This Brick needs some animation effects when it is destroyed. So we need to make an animation.
1. Drag an Animation from Orthello> Objects> Sprites to the Hierarchy view. This will add an Object named "Animation (id =-4320)" under OT-> Animations. Rename it "level anims ".
2. Select the newly added OTAnimation and set the attributes as per.
3. Set the Size to 3 under Framesets.
4. Drag level from OT-> Containers to the Container attribute under OTAnimation.

5. Find the AnimatingSprite in Orthello-> Objects-> Sprites and drag it to the Hierarchy view. This should add an Object similar to "Animating Sprite (id =-23050)" in the scene. Rename it to "brick ".
6. Select the newly added Animating Sprite and drag the previously created "level anims" to its Animation attribute. After dragging, the property Sprite Container should be automatically filled with "level" container. If not, you can manually drag the Sprite iner of "level.
Now, after clicking Play, you should be able to see bricks in the scene. It will execute all frames in the Animation. If you do not want it to be executed at the beginning of the game, deselect Play On Start.
Note: if you cannot see bricks in the Game view, make sure you have set the Scale of bricks to 1, 1, and 1 (sometimes when we create an Orthello Object, its X and Y scales may become very small ).
Run:

Add collision to bricks
We need to add collision bodies to the bricks so that our roles can stand on them.
1. In the Hierarchy view, select "brick" and select "colretriable. This will automatically add a Box Collider and Rigidbody component on Sprite.
2. We also need to add a specific tag for the Object. In Edit-> Project Settings-> Tags, add "Ground" Ladder "and" Rope.
3. Similarly, we add two Layers "Ground" and "Ladder" in Layers ".

4. Select "brick" and change its Tag and Layer to "Ground ".

My CSDN blog address:... the remaining full text>


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.