Unity5 Official tutorial Note (2D Rogue like) 05--player

Source: Internet
Author: User

After the author's observation, found that the main difference between enemy and the player is reflected in the script, the remaining parts of the creation process is basically consistent, so here is the script-based.

Conventions, the main structure of code:

You can see that in addition to some of the features available in the Movingobject, there are some additional things to deal with. Let's explain it in order.

In the Start () method:

In addition to capturing the player's own animator, we also control the text in the UI.

Ondisable () and ontriggerenter2d () are all events provided by the Monobehaviour,

When we switch in the game level, we will give the player to disable off, this time will trigger the Ondisable event, where we are the last hp passed to Gamemanager, and then the next level of the player created, The HP continuation game will be obtained from the Gamemanager after the previous shutdown.

As the player moves through the other colliding bodies, it will be handled differently depending on the tag of the collider (tag can be set in Unity's Inspector View).

For the player's move behavior, we judge the user's input in update ():

As you can see here, we want to keep the player from moving diagonally (because this is not the same as the game's design), so when we detect a horizontal movement, we move the vertical direction to zero.

Input.getaxisraw () is a method that unity provides to get an inline input so that either the keyboard's arrow keys or the phone's accelerator can be detected as an input method. The input class also integrates a number of other methods for obtaining information on various inputs, please check the official documentation for details.

In the Attemptmove method of overwrite:

Every time we try to move it consumes a bit of physical energy (HP, food), modifies the UI and randomly plays a moving sound. After the move is over, it checks to see if the game has been met (physically 0), and finally marks the end of the player's turn, allowing the computer to control the enemy rounds.

In Oncantmove, we will only handle collisions with the interior walls of the site:

The way to deal with this is to let the player make the excavation action, a certain number of excavation will make the site wall is destroyed.

In the Restart () method:

By allowing the engine to reload the game scene, reset the game data (although I did not call this method here, the main reason is lazy, in fact, in the Gamemanager update () to detect the input of a key, if detected, call this method directly)

Finally in Checkifgameover, we will perform some processing after detecting a total of less than 0 food marks the end of the game:

We're done with movingobject and enemy and player. We can see that since we have set the base class for movingobject, we have reduced the number of tasks that need to be repeated when making two units of the back.

Finally, the enemy and player are dragged from the hierarchy view into Project view, and prefab is created automatically, making it easier for the enemy units to create in bulk.

The next article will explain the sound manager (Soundmanager)

Unity5 Official tutorial Note (2D Rogue like) 05--player

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.