Let's continue to learn unity2d game scene production, this article is divided into the following 3 parts:
· adding Roles and controls
. Add 2D physical Block
· Add 2D effects
By making a mini-game with a mobile hat to pick up the bowling ball, we can learn anything from creating a game object, using a 2D physics engine and making 2D effects.
· adding Roles and controls
1. Create a game lead hat
The hat is made up of hat and hatfrontsprite, so that when the bowling ball is caught in the hat, the hat's properties are shown in the following two images. Hat has a hierarchy of 0, and the hatfrontsprite level is 2, so we can get to the effect by setting the bowling level to 1.
Add a rigid body to the hat and prevent the hat from falling. Select commands in the menu bar, Component--physic 2d--rigidbody 2D (rigid body). In the Inspector view of the hat, set the gravity scale to 0 to prevent the hat from falling. Check freeze Rotation Z to fix the angle of the hat,
2. Create a bowling ball
After creating a bowling sprite, we add a rigid body to the bowling ball so that it has a free-fall effect. Select commands in the menu bar, Component--physic 2d--rigidbody 2D (rigid body). In order to have a collision between the bowling and other collisions, choose Component--physic 2d--circle colider 2D (2D Circular collider).
If you do not add a rigid body to a bowling ball, the bowling ball in the game will not be free to fall.
in the game, we want to create more than one bowling ball, so we want to make the bowling sprite into prefab. You can make a preset by dragging the sprite from the hierarchy view directly into the folder in Project view. As shown in the following:
3. Create scripts to control bowling and hats
Let's create an empty game object Gamecontroller, create a C # script called Gamecontroller, and then drag the Gamecontroller script onto the Gamecontroller object .
Public classGamecontroller:monobehaviour { PublicGameobject Ball; Private floatMaxWidth; Private floatTime =2; PrivateGameobject Newball; //Use this for initialization voidStart () {//Convert the width of the screen to world coordinatesVector3 Screenpos =NewVector3 (Screen.width,0,0); Vector3 Movewidth=Camera.main.ScreenToWorldPoint (Screenpos); //get the width of the bowling ball itself floatBallwidth = Ball. Getcomponent<renderer>(). bounds.extents.x; //Calculate the width of the bowling position instantiationMaxWidth = movewidth.x-Ballwidth; } voidfixedupdate () { time-=Time.deltatime; if(Time <0) { //generates a random number representing the time required to instantiate the next bowling ballTime = Random.range (1.5f,2.0f); //generates a random number within the width of the bowling instantiation position to control the location of the instanced bowling ball floatPosX = Random.range (-maxWidth, MaxWidth); Vector3 spawnposition=NewVector3 (PosX, TRANSFORM.POSITION.Y,0); //instantiate bowling, destroy after 10 secondsNewball =(Gameobject) instantiate (Ball, spawnposition, quaternion.identity); //If the time is not written, the component will be destroyed immediately.Destroy (Newball,Ten); } }}
Then create a script Hatcontroller to control the movement of the hat, and then drag the Hatcontroller script onto the Hat object.
Public classHatcontroller:monobehaviour { PublicGameobject effect; PrivateVector3 rawposition; PrivateVector3 hatposition; Private floatMaxWidth; //Use this for initialization voidStart () {//Convert the width of the screen to world coordinatesVector3 Screenpos =NewVector3 (Screen.width,0,0); Vector3 Movewidth=Camera.main.ScreenToWorldPoint (Screenpos); //Calculate the width of the hat floatHatwidth = getcomponent<renderer>(). bounds.extents.x; //get the initial position of the hatHatposition =transform.position; //Calculate the width of the hat's movementMaxWidth = movewidth.x-Hatwidth; } //Update is called once per physics Timestep voidfixedupdate () {//convert the mouse's screen position to world coordinatesRawposition =Camera.main.ScreenToWorldPoint (input.mouseposition);//set the position where the hat is going to move, the CAP movement range controlsHatposition =NewVector3 (rawposition.x, HATPOSITION.Y,0);//limit value values between min and Maxhatposition.x = Mathf.clamp (hatposition.x,-maxWidth, MaxWidth); //Hat MovementGetcomponent<rigidbody2d>(). Moveposition (hatposition); }
}
Now, in the game we can see that the bowling ball falls from a random place in the sky, but behind the bowling ball, it crosses the lawn. The hat will move around with the mouse.
· Add 2D physical block
first add the Edge Collider to Hatfrontsprite, Component--physics 2d--edge Collider 2D (2D Edge Collider). Then add a trigger to hat to determine if the bowling ball is entering the hat, and select the IS Trigger check box in the Inspector View . Add an edge Collider component to hat, and before editing edge Collider 2D, click the Edit Collider button to wrap the edge Collider components in hat. The editing process to be patient, need to constantly adjust, learn to edit, it is best to record the editing process as a video screen, after forgetting the words, you can follow the video screen to operate. As shown in the following:
In order for the hat to continue to be bowling, we have to remove the bowling ball after entering the trigger. So we add the following code to the Hatcontroller script:
void ontriggerenter2d (collider2d col) { // Delete the Colliding object Destroy (col.gameobject); }
In order for the bowling to fall on the grass, we have to add a collision body to the lawn. Gameobject--create empty, named Ground,component--physics 2d--box Collider, sets ground properties such as:
Now, the bowling ground will be blocked by the ground, we move the hat to the bowling ball, regardless of whether you received a bowling ball, after a period of time, the bowling will automatically disappear.
· Add 2D effects
In order to have a spark particle effect when the hat gets a bowling ball, let's now make a simple effect. Import the particle effect resource bundle into the project first, and click the Import button.
Import Effects Resources after that, set the Renderer property in the Inspector panel of the effects particle preset body.
The particle effect code that is generated after the hat is added to the bowling Hatcontroller.
Public classHatcontroller:monobehaviour { PublicGameobject effect; PrivateVector3 rawposition; PrivateVector3 hatposition; Private floatMaxWidth; //Use this for initialization voidStart () {//Convert the width of the screen to world coordinatesVector3 Screenpos =NewVector3 (Screen.width,0,0); Vector3 Movewidth=Camera.main.ScreenToWorldPoint (Screenpos); //Calculate the width of the hat floatHatwidth = getcomponent<renderer>(). bounds.extents.x; //get the initial position of the hatHatposition =transform.position; //Calculate the width of the hat's movementMaxWidth = movewidth.x-Hatwidth; } //Update is called once per physics Timestep voidfixedupdate () {//convert the mouse's screen position to world coordinatesRawposition =Camera.main.ScreenToWorldPoint (input.mouseposition); //print (rawposition); //set the position where the hat is going to move, the CAP movement range controlsHatposition =NewVector3 (rawposition.x, HATPOSITION.Y,0); //print (hatposition); //limit value values between min and Maxhatposition.x = Mathf.clamp (hatposition.x,-maxWidth, MaxWidth); //Hat MovementGetcomponent<rigidbody2d>(). Moveposition (hatposition); } voidontriggerenter2d (collider2d col) {Gameobject neweffect=(Gameobject) Instantiate (effect, transform.position, effect.transform.rotation); Neweffect.transform.parent=transform; //Delete the Colliding objectDestroy (Col.gameobject); Destroy (Neweffect,1.0f); }}
Add a particle effect component to the Hatcontroller script.
Final game effect:
Create a basic 2D scene (Part2)