"Unity3d" Turn System game __unity3d

Source: Internet
Author: User

Round game has been in the history of the game, at least in China's game history plays a very important role. From the Chinese paladin to the dream, this kind of game is loved by the player. So how to achieve it in Unity3d. Here is a relatively simple unity3d game to illustrate this problem. In fact, it is not difficult, the key is to clarify the various processing relationships.

As shown in the following figure, the green on behalf of the player to control the protagonist, Blue represents the enemy, respectively, to give everyone 100HP, and then the player hit the enemy, the enemy is -40hp, the player was -30hp the enemy touch. Here is the player's success in defeating the enemy.


Of course, the player can also defend, at this time the enemy touch player only 15HP. The following figure shows how the player hp turned 0 and the game failed.


Of course, this example is not fun, after all, there is no medicine, only 1 moves, or 1 to 1 of the rally. Not even a MP. There is no calculation according to the speed who first shot the problem, there are buff and debuff and so on. However, in order to illustrate how to make a turn game in Unity3d. I try to simplify the things that can be simplified first, the main focus of the game's production core.

first, Scene layout

The first is a simple scene layout, in the 3D section is very simple. For a few simple basic components, put 2 cube on top of a plane. and the solid color material of different colors. Do not repeat, do not understand can refer to the "Unity3d" objects, material settings, displacement and rotation of objects (click to open the link). The only thing that needs attention is to change the two cube names so that you don't know which one to follow.


Next is the arrangement of Ugui. In the lower left corner is a panel named Actionpanel with two buttons attack button and the Defend button, while Actionpanel will be controlled while button attack buttons and defend The button will give the Click event. Ugui button Click events can refer to "Unity3d" scene switching, Ugui component events, starting games and closing games (click to open the link). Below this actionpanel is a text called Playerhpinfo, which is also controlled by the script to display information such as blood volume.

As for the upper right corner is a dynamic text of the scrolling area Warinfopanel, which placed a warinfotext used to display the battle information text, the specific production can refer to the "unity3d" dynamic text of the scroll bar (click to open the link), Here you need to pay attention to the Mask components of the time to remove show Mask Graphic, or Warinfotext will not appear. And underneath it is a button to quit the fight Exitbutton, of course this thing, in the actual game can not, automatically switch back to the scene before the battle.


and create an empty object Warcontrol at the same time, give script WarControl.cs.

Here are the dependencies of each object, please be careful to change your name. Because basically the above mentioned components, will be WarControl.cs control.


ii. Script-writing

WarControl.cs the variable you set, and the object you want to control looks like this:


The idea of this code is as follows:

Because Update () is performed on every frame refresh, in 1 seconds on the 30 frame of the moment, Update () inside the code does not read, the game is stuck, so Update () this can be regarded as the main thread of the function, only assume the following simple tasks, at all times to determine whether HP see bottom.


And attack performance these to explain to the player to look at the thing, at least occupy 1s skill performance, we through the coroutine complete, the detailed description of the process can see the "Unity3d" coroutine the Use of "(Click to open the link)." A coprocessor, in fact, is a unity3d thread that is created by clicking Time on the button. After each button clicks, the concrete thought follows the figure to indicate, in which the solid line indicates that the player clicked "The attack button", the dash line indicated the player clicked "the defensive button". On the example of animation, I used the unity3d in the extremely simple animation components Itween to do, you can see the "Itween" single point of movement and rotation (click to open the link).

This involves hanging 0.5s~0.9s things, therefore, can only write in the process of completion, it is impossible to write in the update () inside, otherwise the game is absolutely stuck to death.


Therefore, the WarControl.cs is given as follows, giving Warcontrol to an empty object.

Using Unityengine;
Using Unityengine.ui;

Using System.Collections;
    public class Warcontrol:monobehaviour {public Gameobject player;//represents the player's green cube private int player_hp;//player's HP The public Gameobject enemy;//represents the player's blue cube private int enemy_hp;//The enemy's HP public gameobject actionplane;//the lower left corner player operator panel, which has two Button public Gameobject playerhpinfo;//in the lower left corner of the player's HP message text texts public Gameobject warinfotext;//upper right corner battle information texts text public Ga Meobject exitbutton;//exit button private int player_max_hp;//player Max Blood volume, this can actually be regarded as a constant const//scene initialization process, the data initialization process I also wrote here * * VO

        ID Start () {Time.timescale = 1;//break the time boundary, mainly with the following update () in the layout of the time boundary Time.timescale = 0; Players Click "Exit" to re-enter the scene
        /* Define the amount of blood and the maximum amount of blood of the player and the enemy, this part in the actual, can be recorded in the game state of the XML and other places to take, here roughly defined as 100*/player_hp = 100;
        ENEMY_HP = 100;

        PLAYER_MAX_HP = PLAYER_HP;
        /* Update ui*/playerhpinfo.getcomponent<text> (). Text = "HP:" + player_hp + "/" + player_max_hp;//player HP Info Text update Warinfotext.getcomponent<text> (). TeXT = "battle begins."
    \ n;//Battle Information update exitbutton.setactive (FALSE);//Hide "Exit Fight" button}//main thread, always read, this section because a lot of code is the same, so you can optimize the writing of this conditional structure * * void Update () {if (player_hp < 0) {playerhpinfo.getcomponent<text> (). Text = "HP:" + player_hp + "/" + player_max_hp;//due to combat settlement in the following child threads complete, in the final battle settlement need to update the UI again to avoid having to show bugs Time.timescale = 0;//decorate A Time bounded exitbutton.setactive (true);//Open the "Exit Game" button player.setactive (false);//will represent the player this cube disappears and can actually play the player dead Actionplane.setactive of what the animation is (false);//Close the Operation UI/* Update battle Information * * Warinfotext.getcomponent<tex
            T> (). fontsize = 30; Warinfotext.getcomponent<text> (). Text = "player dies." The battle failed.
        \ n "; if (ENEMY_HP < 0)/IBID., do not repeat the {playerhpinfo.getcomponent<text> (). Text = "HP:" + Play
            ER_HP + "/" + player_max_hp;
            Time.timescale = 0;
            Exitbutton.setactive (TRUE);
            Enemy.setactive (FALSE); ActIonplane.setactive (FALSE);
            Warinfotext.getcomponent<text> (). fontsize = 30; Warinfotext.getcomponent<text> (). Text = "enemy death." Victory fight.
        \ n ";
    }/* button click event/public void Attackbuttononclick () {Startcoroutine (Attack ());
    public void Defendbuttononclick () {Startcoroutine (defend ());
    public void Exitbuttononclick () {application.loadlevel ("Turnbase_single"); }/* Attack coprocessor/IEnumerator Attack () {actionplane.setactive (false);//Close the Operation UI Startcoroutine first (Playe  R_attack ())//Create a new player attack yield return new waitforseconds (0.9f);/wait for 0.9s to read the following code, that is, waiting for the player to attack the skills performed, altogether 0.9s yield return new Waitforseconds (0.5f), and then wait for 0.5s, let the player take a breather, said that the action is finished, began to account for the following enemy attack Skills Startcoroutine (Enemy_attack (false)); Create a new enemy attack, where false on behalf of the player is not defensive yield return new waitforseconds (0.9f);/wait 0.9s to read the following code, that is, waiting for the enemy attack skills performed, altogether 0.9s P Layerhpinfo.getcomponent<text> (). Text = "HP: "+ player_hp +"/"+ player_max_hp;//Update UI actionplane.setactive (TRUE);//Then open the Operation UI and let the player make the next turn instruction yield ret
    Urn null; }/* Defense CO/IEnumerator defend () {actionplane.setactive (false);//Close the Operation UI Startcoroutine first (enemy _attack (TRUE);/create a new enemy attack, where true means that the player is not defensive yield return new waitforseconds (0.9f);/wait for 0.9s to read the following code, that is, waiting for enemy attack skills to perform, Altogether 0.9s playerhpinfo.getcomponent<text> (). Text = "HP:" + player_hp + "/" + player_max_hp;//Update UI Actio
    Nplane.setactive (TRUE);//Then open the Operation UI, let the player for the next round of instructions yield return null;  /* Player attack skills performance, with Itween implementation */IEnumerator Player_attack () {Itween.moveto (player, Itween.hash ("position",
        New Vector3 (0, 0.5f, 2), "Easetype", "Easeincubic", "Time", 0.3f, "Looltype", "none"));
        Yield return new waitforseconds (0.3f); Itween.rotateto (Player, Itween.hash ("Rotation", new Vector3 (0, 180, 0), "Easetype", "Easeincubic", "Time", 0.3f, "
        Looltype "," none ")); Itween.rotaTeto (Enemy, Itween.hash ("Rotation", new Vector3 (0, 0), "Easetype", "Easeincubic", "Time", 0.3f, "Looltype", "none"));
        Yield return new waitforseconds (0.3f); Itween.moveto (Player, Itween.hash ("position", new Vector3 (0, 0.5f,-4), "Easetype", "Easeincubic", "Time", 0.3f, "
        Looltype "," none ")); Itween.rotateto (Enemy, Itween.hash ("Rotation", new Vector3 (0, 0, 0), "Easetype", "Easeincubic", "Time", 0.3f, "Looltype"
        , "none")); Itween.rotateto (Player, Itween.hash ("Rotation", new Vector3 (0, 0, 0), "Easetype", "Easeincubic", "Time", 0f, "Looltype",
        "None"));

        Yield return new waitforseconds (0.3f);
        /* Attack settlement/warinfotext.getcomponent<text> (). Text + "Player attack, enemy -40hp\n"; This.

    ENEMY_HP-= 40; /* * Enemy attack skills performance, with Itween implementation */IEnumerator Enemy_attack (bool isplayerdefend) {Itween.rotateto (enemy, Itwe En.
        Hash ("Rotation", new Vector3 (0, 180, 0), "Easetype", "Easeincubic", "Time", 0.3f, "Looltype", "none"));Itween.rotateto (Player, Itween.hash ("Rotation", new Vector3 ( -30, 0, 0), "Easetype", "Easeincubic", "Time", 0.3f, "
        Looltype "," none "));
        Yield return new waitforseconds (0.3f); Itween.rotateto (Enemy, Itween.hash ("Rotation", new Vector3 (0, 0, 0), "Easetype", "Easeincubic", "Time", 0f, "Looltype", "
        None ")); Itween.rotateto (Player, Itween.hash ("Rotation", new Vector3 (0, 0, 0), "Easetype", "Easeincubic", "Time", 0.3f, "Looltype
        "," none ")); /* Attack settlement/if (!isplayerdefend) {warinfotext.getcomponent<text> (). Text = "Enemy attack, player -30hp\n
            "; This.
        PLAYER_HP-= 30;
            else {warinfotext.getcomponent<text> (). Text = "Enemy attack, player -15hp\n"; This.
        PLAYER_HP-= 15;
    } yield return null;
 }

}

It's more than 100 lines of code. The attack animation here, using the Itween implementation, you can see the "Itween" to complete multiple actions, Itween sequence of action (click to open the link), do not repeat. At the same time, the player dead and the death of the enemy in fact can also add a cube fragmented animation, so that the game more vivid, specific reference to the "Fracturing & Destruction" in the ball--unity3d to meet the conditions before the trigger of the object burst, burst, fragmentation effect (Click to open the link), here to illustrate the problem, I do not engage in such a complex, pull so many irrelevant plug-ins come in, reduce the readability of the code.

Also given to Attack_button, Defend_button and Exitbutton, the three button click events were WarControl.cs Attackbuttononclick (), Defendbuttononclick ( ) and Exitbuttononclick () are done.


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.