Unity Learning Notes -2d Horizontal version of the game in the background mobile __unity3d

Source: Internet
Author: User
2d horizontal version of the game, the background can be indefinitely continued. Today, a relatively rudimentary version has been implemented.
Just learn to write the script in unity.
First set two ground, the window displayed, that is, the player is currently standing on the ground, named Leftground, on the right side next to the Rightground.
Naming rules what not to tangle, after all, is testing.
In addition, no need to deliberately distinguish two ground who is left who is right.

Move Rule : When the player moves to the right, 2 ground moves to the left, the distance of course is the player's speed of movement.
Take the ground to the right as an example: when a ground's x coordinates exceed the x maximum in the screen, the ground needs to be reset to the left.
The treatment of two floors is the same. In the process of moving, two of the ground will have a change of position, so, it is impossible to distinguish who left the right.
In fact, there is no need.

Then you write a mobile control script, the script is bound to the player, after all, the trigger of the move is the player initiated.

Using Unityengine;

Using System.Collections; public class Scenemove:monobehaviour {private bool M_bmove = false;/whether to move background private bool M_bleft = true;//true represents Right-to-left	Move left, the character is moving to the right public float m_speed = 2.0f; Mobile speed public tk2dtiledsprite m_groundleft;
	Left ground public tk2dtiledsprite m_groundright;//right ground private float M_fminx = -13.2f;

	private float M_fmaxx = 13.2f; Use this for initialization void Start () {}//Update are called once per frame void Update () {if input.ge
			Tkeydown (Keycode.a)) {m_bleft = false;
		M_bmove = true;
			else if (Input.getkeydown (KEYCODE.D)) {m_bleft = true;
		M_bmove = true;
		} if (M_bmove) {movebackground (time.deltatime);
		} void Movebackground (float fdelta) {float fmovespeed = m_speed * FDELTA; if (m_bleft) {m_GroundLeft.transform.Translate (Vector3.left * fmovespeed, Space.world);//Move left M_groundright.trans Form.
			
			Translate (Vector3.left * fmovespeed, Space.world); if (m_groundleft.transform.Localposition.x <= m_fminx) {m_GroundLeft.transform.localPosition = new Vector3 (M_fmaxx, 
				                                                   M_GROUNDLEFT.TRANSFORM.LOCALPOSITION.Y,
			M_GROUNDLEFT.TRANSFORM.LOCALPOSITION.Z); } if (m_groundright.transform.localposition.x <= m_fminx) {m_GroundRight.transform.localPosition = new Vector3 ( 
				                                                    M_fmaxx, M_GROUNDRIGHT.TRANSFORM.LOCALPOSITION.Y,
			M_GROUNDRIGHT.TRANSFORM.LOCALPOSITION.Z);
			} else {m_GroundLeft.transform.Translate (Vector3.right * fmovespeed, Space.world);
			M_GroundRight.transform.Translate (Vector3.right * fmovespeed, Space.world); if (m_groundleft.transform.localposition.x >= m_fmaxx) {m_GroundLeft.transform.localPosition = new Vector3 (M_fminx , M_GroundLeft.transform.loCALPOSITION.Y, M_GROUNDLEFT.TRANSFORM.LOCALPOSITION.Z); } if (m_groundright.transform.localposition.x >= m_fmaxx) {m_GroundRight.transform.localPosition = new Vector3 (
				                                                    M_fminx, M_GROUNDRIGHT.TRANSFORM.LOCALPOSITION.Y,
			M_GROUNDRIGHT.TRANSFORM.LOCALPOSITION.Z);
 }
		}
	}
}
After the script is bound to the player, you need to set the following:

Run the game, haha, no problem. Press A to move the player to the left and press D to move the player to the right.

Note: I have a small picture of the ground here, so I used tk2dtiledsprite for convenience, and then the M_fminx and M_fmaxx in the script are the scaling coefficients of the individual ground.
It's not yet known how to get these two ground objects in a script. Go on with your study.

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.