"Learn Unity3d with Me" as a 2D 90 tank battle director and prop system

Source: Internet
Author: User

Playing games I still used to use a Director class to control the process of the game, such as the beginning and end of the game, the game of AI, the game map of the drawing and so on are placed in such a director class inside. and then this director class I put it in Maincamera, as its component.

First, in start, you need to initialize some of the game's data, such as reading the map, initializing the AI, etc.

    void Start ()    {//Initialize data        m_iaicurtankcount = 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 map        M_sxmlpath = application.datapath + "/map/map_1.xml";        Read map from XML        readfromxml ();        M_gmytank.getcomponent<cmytank> (). m_fmovespeed = m_imymovespeed;//Create AI while        (M_iaicurtankcount < m_ Iaitankcount)        {            startcoroutine (Createai (1.0f));        }        Place the prop invokerepeating every 30 seconds        ("Createprop", +);    }

The point here is the Startcoroutine function, which is used to start a collaborative program, what is a synergistic program, in my understanding, the cooperative program is that this section of the function is executed immediately, does not block the thread, but if the section of the function has the yield keyword, Yield the following function will be executed only after the execution of the statement of yield is guaranteed to finish. The following is an example of the implementation of 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: This segment of the statement will execute after a delay        of waittime seconds 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 a delay of waittime seconds    }
The initial position of AI is pseudo-random, and the AI is generated in three places at the top of the map, where the Randdirection function is implemented as follows:
    Randomly generates a 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 is the creation of props, the method of creating props and the front of the firing bullets is the same, starting 15 seconds after the creation of the first props, behind every 30 seconds to create the back of the props. The specific implementation of the function Createprop for creating props is as follows:

    void Createprop ()    {        Gameobject Temp = Instantiate (M_gprop, Randposition (), quaternion.identity) as Gameobject;        Destroy (Temp, 20.0f);    }
function is randomly generated a prop, the position of the prop is random on the map, the props will be automatically eliminated after 20 seconds, on the map random function Randposition implementation is 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 then each grid is 0.6, the center of the map is the origin of the coordinates, why the z-axis is set to -2.0f, because this will ensure that the item will always float on the map (the grass Z axis is -1.0f).

Props I only made a prop, is the bomb, after the props, all the AI will explode. The implementation of this prop I have made a separate script, specifically implemented 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);        The player eats the prop        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 properties to the props later.


Read the map this piece has already been told in the previous blog, and will not be said.

"Learn Unity3d with Me" as a 2D 90 tank battle director and prop system

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.