Since the problems encountered in development require a large text volume description, the entire process of development is described, along with the problems and workarounds that are encountered with red flags .
The development process is as follows:
1. Download the model and footage you need for the game and import it into Unity.
2. Add player aircraft
Add the player's plane to the hierarchy view (Hierarchy) in the footage and name it as player. The Hierarchy View (Hierarchy) contains each game object (Gameobject) in the current scene. Some are direct instances of resource files such as three-dimensional models, others are preset (prefabs) instances, and custom objects make up the majority of the game. A preset (Prefab) is a resource that can be reused in Project view (Gameobject) in a hierarchical View (Hierarchy) and drag an object into another object to apply a parent-child (parenting) (see). After you add and delete objects in the scene, they are also displayed or disappear in the hierarchy View (Hierarchy).
When viewing a game object, you can use qwer to rotate, move, and zoom the viewing angle using several shortcut keys.
In view of the aircraft war game will involve collisions and other events, and the aircraft model is composed of too many triangles, do collision detection will lead to inefficient problems arise. Therefore, we add an aircraft collision model to the player, which is added to the aircraft model's grid Collider (Mesh collider).
At the same time, due to the presence of gravity in the plane itself, it is free to fall in the three-dimensional space in which it is run, and we want the plane to move in a plane, so we remove the gravitational effects in the player's rigid body nature (rigidbody).
Finally, we add a Spitfire model to the player and adjust the position of the fire model. The aircraft model was made .
3. Camera settings
Camerais a device that captures and displays the world to players. The camera's default configuration does not conform to the player's view of this horizontal version of the game, so we adjust the camera's parameters. First, the default camera projection (Projection) is perspective (perspective), i.e. the camerawill keep objects and perspectives intact. We need to tune it to orthogonal (orthographic), that is, the camera will render the object evenly, without a sense of perspective. Second, the default camera background is blue and does not conform to the perception that space is black, so adjust accordingly. Finally, we set the camera perspective to look down on the plane, similar to the classic lightning game perspective.
4. Add a light effect
After the first few steps, we found that the color of the plane and scene was very dark, so we had to set the lights to beautify the game interface. According to the adjustment, we set up three lights to make the plane achieve a better effect.
5. Add a background
In the Hierarchy view (Hierarchy), add a Quad object named Background. The Quad object is a four-sided shape consisting of two triangles for mapping. Adjust The parameters of the quad so that it is positioned just below the player and add a background picture from the footage. Adjusts the size of the background and the relative position of the aircraft relative to the background, depending on the pixel size of the footage image itself.
Create a Gamecontroller script for the entire game initialization, restart, interface hint text, scoring function, scene control and so on.
6. Player Aircraft control
Create a Playercontroller script for the player to set up a series of actions.
① Mobile
Set the speed of an airplane, and then get the two values H and V in horizontal direction and vertical direction respectively . the value of rigidbody the plane rigid body is the speed multiplied by h or v.
② Moving Range
The plane's moving range must be within the context, adding a serialized bound class to the script, setting the xMin,xmax,ymin, and ymax four values, and taking the values based on the parameters of the background graph in the three-dimensional space. Serialization attributes allow us to implant a class with alternative content in the inspector so that you can set the move range in the Unity interface without having to re-set it in the script, improving the convenience of the changes. Set the position of the aircraft rigid body rigidbody, use the mathf.clamp function to limit value between min and Max, and return min if value is less than min . If value is greater than max, returns Max, otherwise returns value, which controls the plane's moving range within the background range.
③ Creating bullets
add a bullet bolt object in the hierarchy view (Hierarchy) , select the laser effect from the footage to add the Quad, and adjust the quad position so that the bullet is at the aircraft launch port. Creating a Mover script, similar to setting up the movement of the airplane, we set the bullet to have speed in the Y direction and faster than the plane's y direction. Since bullets hit objects, we also set up collision detection for bullets, a collision model for bullets, and a mesh collider (mesh collider) that adds bullet models .
At the same time, we add a rigid body to the bullet and remove gravity.
We set the Istrigger property of the bullet, since the bullet will cause an explosion when it touches an object . If this property is checked, then the object is a virtual body, tangible without real, force-free, and other objects can pass through it, but if the collision event conditions are met, then the object will produce a trigger event.
Finally, the Bolt object is added to the pre- set (prefabs) example for reuse.
④ fired Bullets
When used, the bullets are added to the player's plane, set in the Playercontroller script, and the bullets are fired when the left mouse button is pressed. At the same time, we set a transmit frequency and the next launch time. When the next launch time is greater than or equal to the allowable launch interval, the projectile is allowed to be fired, thus controlling the firing frequency.
⑤ aircraft explosion
Principle with the rear meteorite explosion.
7. Boundary setting
Since a bullet will create an increasing number of instances without automatically disappearing, we have to set a cube object on the plane of the bullet as the boundary and destroy the bullet when the bullet goes out of bounds.
Create a destroybyboundary script that, when the bullet collider leaves the boundary trigger, calls Ontriggerexit to destroy the bullets in the function.
8. System Meteorite Control
① Creating meteorites
Meteorites are "enemies" that players want to dodge and attack. We chose three different shapes of meteorites to add to the footage:
Add three meteorite objects to the hierarchy view (Hierarchy), place them on a different side of the plane, and set up a rigid body, trigger, and so on, similar to airplanes and bullets. to make the game scene more realistic, we set up meteorites to be randomly rotated. Create a randomrotater script, set the angular velocity of the meteorite rotation, and use Random. Insideunitsphere Returns a random point in the sphere with a radius of 1 to achieve a random rotation effect.
At the same time, meteorites should have the opposite speed to the bullets, so we assign the mover script to the meteorite object and set the speed of the meteorite movement.
② meteorite explosion
When a meteorite hits a plane and a bullet, an explosion occurs. Create a script destroybycontact that calls Ontriggerenter when the collider touches the trigger . If there are other objects touching meteorites, destroy other objects with meteorites themselves. at run time, we encounter the problem of destroying the meteorite at the beginning of the game, because the meteorite was in the boundary at the very beginning and would destroy both the boundary and the meteorite itself. Therefore, we add a tag tag for the boundary, and add the judging condition in the script destroybycontact, if it encounters the boundary, it returns directly, and does not destroy any object.
Create a meteorite blast effect object, which produces an explosion when the meteorite is destroyed.
Add the script to the properties of three meteorites.
③ Meteorite appeared
create an array of meteorites in the Gamecontroller script. Use random numbers to choose which meteorites to appear. In the update function, the control random number is less than a certain value, which controls the occurrence of a meteorite in the X-direction at a certain space interval.
We use yield return in the unity Process (Coroutin) method to return the IEnumerator object only if the object's MoveNext () returns false, which is the The statement after yield return is executed only if the current of ienumertator has been iterated over to the last element. In other words,yield return is "translated " into a Ienmerator object.
Using this feature, we use the loop to control the number of meteorites that occur in each batch of "meteorite attacks" and set the wait in the loop, in which a meteorite appears every other time in a batch of meteorites. Wait for a certain amount of time between each occurrence of the batch and at the beginning of the game.
9. Sound
① meteorite explosion
Add the sound of a meteorite explosion from the footage and tick play on awake, which will play an exploding sound when the explosion effect occurs. Add explosive sound to three meteorites.
② background music
Same as above, placed in Gamecotroller, where the game plays music from the beginning and is set to loop.
③ fired Bullets
set fire variables in the Playercontroller script to play sound when bullets are fired. and perform the same operation as above.
10. Destroying the explosion effect
Create a destroybytime script that sets the life cycle for the explosion effect and destroys it over the life cycle.
11. Interface text and its related functions
Create the UI text and rename it to Display text.
① scoring function
create scoring features in the Gamecontroller script. Because hitting a meteorite will add points, and judging if the meteorite is hit is obtained in the destroybycontact script, so we set Gamecontroller object in the script to implement the Gamecontroller to find information about whether to hit meteorites. In the inspector, different settings are made for the scores that hit different meteorites.
② Game Over
At the end of the game we have to tell the player that the game is over and tell him how to start again.
make the corresponding text display settings in the Gamecontroller script. At the same time, an interrupt is set in the function that generates the meteorite, i.e. no meteorites are generated when the game is finished.
In the destroybycontact script, when a meteorite touches a player, it calls Gamecontroller to end the game.
③ Start again
In the gamecontroller script, when the game is over and the player presses the R key, the app's Loadlevel function is called to start the game again.
MA billion
Unity Game Space Shooting