Unity5 and Ugui's little demo snake

Source: Internet
Author: User

Have time to learn Unity5 practice practiced hand, a snake small demo

Simple 2D scene, objects are sprites, need to set the display level before and after, from the snake head to the tail of the order in the Layer value is 1000 decrement 1, so the effect is displayed with scales of the cascade.

Every section of the snake has a script SnakePart.cs records the location and direction of this section, including calculated values.

The snake does not eat an apple, it will instantiate the resulting section, where the newly generated section has a new script NewSnakePart.cs the main function is to initialize this section to the end of the snake including the initialization position, direction.


GameManager.cs Singleton class,list<snakepart> This is all the knots of the snake, there are randomly generated food, the game ends, restarts, the position of each section is converted (from the tail to the end of the conversion).

Finally is the PlayerScript.cs script, is hanging on the head of the snake, first said to do the game, is the mouse (or finger) to drag the snake head, so the snake will follow the move.  Then the purpose of this script is to calculate the distance and direction that each section should move forward. There is the collision detection, testing to eat apples, wall and bite themselves.

In the game only the snake head is both Rigidbody and Collider, the other only Collier, this is the Unity Physics engine collision detection condition, must both have the collision body, and is the moving object must have the rigid body.


As for the Gameover UI is the use of Unity5 native Ugui at the mercy of a bit.

The script is less different than the previous version of Unity5.


Finish

??

Unity5 and Ugui's little demo snake

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.