Just learned unity3d, followed by the imitation of flappy bird, write down some trivial experiences!

Source: Internet
Author: User

1. Scenario: scene.
A normal game must have at least three scenarios, namely the menu (or directory) scenario, game level scenario, and game end scenario. They are generally stored in the scene directory (created by yourself) under the project directory for convenient management.
1.1 scenario switching: To achieve normal game operation, scenario switching is required. For example, how to enter the game from the Start Menu.
Start Menu

Level Screen
Two steps are required: 1. Set
You need to place the scenario in the indicated position and mark it.
2. Write related Code

// Material: public Texture startButton; public Texture rankButton; void OnGUI () {// start menu to start the game. Before rect, you must have a new value and the rect parameter is left and top, button size if (GUI. button (new Rect (100,250,135, 75), startButton) {Application. loadLevel ("Start");} GUI. button (new Rect (460,250,135, 75), rankButton); if (GUI. button (new Rect (20, 20, 60, 30), "exit") {Application. quit ();}}

The Application. LoadLevel ("Start") function is used for scenario switching. Start is the name of the scenario to be switched. Of course, the number 1 can also be used here, because the Start label is 1.

Scenario switching is usually accompanied by an event. The above is because a startButton event occurs.

Note:
If (GUI. Button (new Rect (100,250,135, 75), startButton) {} is used to determine whether to click the Button,
The GUI. Button (new Rect (100,250,135, 75), startButton); generates the Button.


1.2 What causes switchover
In general, clicking a button, other triggers, and collision servers can also lead to switching (we have learned this for the time being)
// Birds collide with pipelines, the game ends void OnCollisionEnter (Collision other) {if (other. gameObject. tag = "Player") {GameManager. _ intance. gameState = GameManager. GAMESTATE_END; audio. play (); Application. loadLevel ("End"); if (other. gameObject. tag = "projectile "){}}}

The OnCollisionEnter (Collision c) function is used to detect the response of the Collision generator. Similar functions include OnCollisionStay and OnCollisionExit, which respectively indicate that the Collision generator is in the contact, in the Collision generator, and leaves the Collision generator, in this way, you can make corresponding judgments based on the situation. Here, birds and pipelines are all collision machines (it seems that at least one must add the Rigid Body Component rigidbody). When they collide, they will respond accordingly: 1. First, identify the collision pipeline (the code is part of the pipeline) and use tags to differentiate objects.
2. Perform subsequent processing. Here, audio. play is used to play music. You need to add the component audioSource to the MPs queue and bind the music (because the code is in the MPs Queue ). LoadLevel () y switching scenario.
2. Moving Objects 1. Moving birds
Bird has rigid body components such
public void getLife(){rigidbody.useGravity=true;this.rigidbody.velocity = new Vector3(2,0,0);}

There is no gravity at the beginning (because it is set to be activated only after the mouse is clicked when the bird does not act when it enters the game), The rigidbody of the getlife function. useGravity = true enables the bird to enable gravity, and then this. rigidbody. velocity = new Vector3 (2, 0, 0); gives it the initial speed in the horizontal (X axis) Direction. Velocity describes the current relative speed of a role.
<Pre name = "code" class = "csharp"> // jump up void birdJump () {if (Input. getMouseButton (0) {// left mouse button downaudio. play (); Vector3 vel2 = this. rigidbody. velocity; this. rigidbody. velocity = new Vector3 (vel2.x, 5, vel2.z );}}

Press the left button to achieve the jump. First, get the current speed, and then change the speed in the y direction (the actual situation varies) to achieve the upward action.
In this way, we can combine the above Code to realize the movements of birds.

2. Implementation of horizontal movement 
Void Update () {// determine if (GameManager. _ intance. score> = minScore) renderer. enabled = true; float outToMove = speed * Time. deltaTime; transform. translate (Vector3.up * outToMove); // resend if (transform. position. x <= (birdTransform. position. x-2.0f) {setPosition ();}}
float outToMove = speed * Time.deltaTime;transform.Translate(Vector3.up*outToMove);
These two lines of code can be used to move an object in the direction of a certain axis. Check the specific information.

 

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.