Importing footage
Preliminary production object
Next, we want to make the objects in the game. We open the GameResource file under the Assets folder of the Project panel. There are three folders below. From top to bottom, they are sound, font and picture. We find the player's picture and create an entity according to the following methods.
Then, according to this method, the Grass, the Wall that can be pierced by bullets, the Barrier that cannot be pierced by bullets, the air Barrier, and the eagle sign (Heart... Is the nest we want to protect)
Animation effect
Next, let's do moving water, explosion, shield and born! Let's take the explosion effect as an example:
Press and hold Ctrl key to select two pictures of explosion effect at the same time, and then drag them to the directory on the left;
Tank movement
Create a Player script and put it on the tank
I wrote out the final code of the tank to prevent it from being written separately. We can't understand it and it's more convoluted
The code is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
//Bullet Euler angle
private Vector3 bullectEulerAngles;
private float moveSpeed = 3;
private float timeVal;
private float defendTimeVal = 3f;
//Render the material after the tank turns
private SpriteRenderer sr;
public Sprite[] tankSprite; // Upper right lower left
private bool isDefended = true; public GameObject explosionPrefab; public GameObject bullectPrefab; public GameObject defendEffectPrefab; private void Awake() { sr = GetComponent<SpriteRenderer>(); } private void Update() { //Is it invincible if (isDefended) { defendEffectPrefab.SetActive(true); defendTimeVal -= Time.deltaTime; if (defendTimeVal <= 0) { Debug.Log(111); isDefended = false; defendEffectPrefab.SetActive(false); } } } private void FixedUpdate() { if (PlayerManager.Instance.isDefeat) { return; } Move(); //Attack cd if (timeVal >= 0.4f) { Attack(); } else { timeVal += Time.fixedDeltaTime; } } //move public void Move() { float v = Input.GetAxisRaw("Vertical");//Vertical axis value input transform.Translate(Vector3.up * v * moveSpeed * Time.deltaTime, Space.World); if (v < 0) { sr.sprite = tankSprite[2]; bullectEulerAngles = new Vector3(0, 0, -180); } else if (v > 0) { sr.sprite = tankSprite[0]; bullectEulerAngles = new Vector3(0, 0, 0); } //Priority problem, give priority to the vertical direction if (v != 0) { return; } float h = Input.GetAxisRaw("Horizontal");//The horizontal axis value input returns - 1 to 1 transform.Translate(Vector3.right * h * moveSpeed * Time.deltaTime, Space.World); //Adjust direction when moving if (h < 0) { sr.sprite = tankSprite[3]; bullectEulerAngles = new Vector3(0, 0, 90); } else if (h > 0) { sr.sprite = tankSprite[1]; bullectEulerAngles = new Vector3(0, 0, -90); } } //Bullet firing private void Attack() { if (Input.GetKeyDown(KeyCode.J)) { //Instanced bullet generation angle: current tank angle + the angle that the bullet should rotate Euler angle Instantiate(bullectPrefab, transform.position, Quaternion.Euler(transform.eulerAngles + bullectEulerAngles)); timeVal = 0f; } } //Death method of tank private void Die() { if (isDefended) { return;//Skip death method } PlayerManager.Instance.isDead = true; //Generate explosion effects Instantiate(explosionPrefab, transform.position, transform.rotation); //death Destroy(gameObject); }
}
Here is the attribute panel of the tank, assignment. Remember to add rigid bodies and collision bodies to the tank
Change the Gravity Scale in the rigid body to 0, or it will fall down
By the way, lock the Z-axis of the tank
Bullet making
Like other entities, we find bullet style pictures from our material, drag them into the scene, adjust the size ratio to 3:3:3, rename them Playerbullect, and add preforms:
The final code and property panel of the bullet are given below
Create a bullet class
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullect : MonoBehaviour {
public bool IsPlayerBullect; // Use this for initialization void Start () { } // Update is called once per frame void Update () { transform.Translate(transform.up * 10 * Time.deltaTime, Space.World); } //collision detection private void OnTriggerEnter2D(Collider2D collision) { switch (collision.tag) { case "Player": //Hit yourself if (!IsPlayerBullect) { collision.SendMessage("Die"); Destroy(gameObject); } break; case "Heart"://Call home collision.SendMessage("HeartDie"); Destroy(gameObject); break; case "Enemy"://Hit the enemy if (IsPlayerBullect) { collision.SendMessage("Die"); Destroy(gameObject); } break; case "Barrier"://Hit an obstacle Destroy(gameObject); //AudioSource.PlayClipAtPoint(hitAudio, transform.position); break; case "Wall"://Hit the wall Destroy(collision.gameObject); Destroy(gameObject); break; default: break; } }
}
Add labels to corresponding objects
Also add rigid bodies and colliders to bullets
Check the Is Ttigger attribute in the collider
Change the Gravity Scale in the rigid body to 0, or it will fall down
Copy one of our bullets and rename it enemybullet. The only difference between enemy bullets and our bullets is that enemy bullets don't tick is player bullet
When the tank is hit, it will explode (explosion effects and code)
Explosion effects have been made on it. The name is explosion
There is only one code that destroys itself
public class Explosion : MonoBehaviour {
// Use this for initialization void Start () { Destroy(gameObject, 0.2f); } // Update is called once per frame void Update () { }
}
Enemy AI compilation and optimization
Directly replace the material of your own tank with that of the enemy tank
Delete the Player code remove and create an Enemy script
The final code is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour {
//Bullet Euler angle private Vector3 bullectEulerAngles; private float v = -1; private float h; private float moveSpeed = 3; private float timeVal; private float timeValChangeDirection = 4; //Render the material after the tank turns private SpriteRenderer sr; public Sprite[] tankSprite; //Upper right lower left public GameObject explosionPrefab; public GameObject bullectPrefab; private void Awake() { sr = GetComponent<SpriteRenderer>(); } private void Update() { Move(); //Attack cd if (timeVal >= 3f) { Attack(); } else { timeVal += Time.deltaTime; } } //move public void Move() { if (timeValChangeDirection >= 4) { int num = Random.Range(0, 8); if (num > 5) { v = -1; h = 0; } else if (num == 0) { v = 1; h = 0; } else if (num >0 && num <= 2) { h = -1; v = 0; } else if (num < 2 && num <= 4) { h = 1; v = 0; } timeValChangeDirection = 0; } else { timeValChangeDirection += Time.fixedDeltaTime; } transform.Translate(Vector3.up * v * moveSpeed * Time.deltaTime, Space.World); if (v < 0) { sr.sprite = tankSprite[2]; bullectEulerAngles = new Vector3(0, 0, -180); } else if (v > 0) { sr.sprite = tankSprite[0]; bullectEulerAngles = new Vector3(0, 0, 0); } //Priority problem, give priority to the vertical direction if (v != 0) { return; } transform.Translate(Vector3.right * h * moveSpeed * Time.deltaTime, Space.World); //Adjust direction when moving if (h < 0) { sr.sprite = tankSprite[3]; bullectEulerAngles = new Vector3(0, 0, 90); } else if (h > 0) { sr.sprite = tankSprite[1]; bullectEulerAngles = new Vector3(0, 0, -90); } } //Bullet firing private void Attack() { //Instanced bullet generation angle: current tank angle + the angle that the bullet should rotate Euler angle Instantiate(bullectPrefab, transform.position, Quaternion.Euler(transform.eulerAngles + bullectEulerAngles)); timeVal = 0f; } //Death method of tank private void Die() { PlayerManager.Instance.playerScore++; //Generate explosion effects Instantiate(explosionPrefab, transform.position, transform.rotation); //death Destroy(gameObject); } private void OnCollisionEnter2D(Collider2D collider) { if (collider.gameObject.tag == "Enemy") { timeValChangeDirection = 4; } }
}
For the components on the body and those that need to be assigned, change the names to SmallEnemy and BigEnemy, and make one large and one small