Alas, there aren’t many Unity tech blogs out there, so I don’t think anyone has seen this post

Moved, nuggets official actually reply to me [crying]

Immediately the enclosed material link: pan.baidu.com/s/1KiucZebe…

I will modify the source code and add comments and then attach pictures or videos to do a good job in this blog

  1. The import material
  2. Create the Scenes folder and save the current scene
  3. Drag the game terrain into the hierarchy view
  4. Remove lights because the terrain has its own lights
  5. Turn off automatic rendering under the Lighting TAB as the scene is large and time-consuming
  6. Set skybox or solid color sky according to personal preference (main camera and Lighting TAB)
  7. Drag the tank model into the scene and reset the position
  8. Adjust the main camera position and switch to orthogonal mode
  9. Place the smoke prefab behind the gears of the tank
  10. Prefabricated tanks
  11. Add collider components and rigid body components to the tank so that the collider does not overlap with the ground
  12. To create a movement script for a tank, disable the tank rigidbody’s y axis position and x and Z axis rotation
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TankMovement : MonoBehaviour {

	private Rigidbody rigidbody;

	public float speed = 15f;
	public float angularSpeed = 5f;

	void Start() { rigidbody = this.GetComponent<Rigidbody>(); } // The physical update function voidFixedUpdate() {// get inputfloat h = Input.GetAxis("Horizontal");
		float v = Input.GetAxis("Vertical"); Rigidbody. velocity = transform. Forward * v * speed; / / rotating rigidbody. AngularVelocity = the transform. The up * h * angularSpeed; }}Copy the code
  1. To add the Rotation script to the tank turret, add a new input axis called Rotation, select the key control and set it to Q and E
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TankRotation : MonoBehaviour {

	public float rotationSpeed = 3f;

	private float angle = 0;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update() {// Use mouse and buttons to control rotation simultaneouslyfloat mousex = Input.GetAxisRaw("Rotation") == 0 ? Input.GetAxisRaw("Mouse X") : Input.GetAxisRaw("Rotation");
		angle += mousex * rotationSpeed;
		angle = angle < -360 ? angle += 360 : angle;
		angle = angle > 360 ? angle -= 360 : angle;
		angle = Mathf.Clamp(angle, -360, 360);
	}

	void LateUpdate() { Quaternion xQuat = Quaternion.AngleAxis(angle, transform.up); transform.rotation = xQuat; }}Copy the code
  1. Drag the bullet model into the scene, add the capsule collider and rigid body, adjust the direction of the collider to z-axis, the size is appropriate, and delete it after making the preform
  2. Create an empty object under the turret to fire the bullet from, adjust the position to the turret mouth, and rotate the direction to fire
  3. Added attack scripts to tanks
using System.Collections; using System.Collections.Generic; using UnityEngine; Public class TankAttack: MonoBehaviour {public Transform firePosition; Public GameObject shellPrefab; // Bullet speed publicfloat shellSpeed = 15f;

	void Update() {// Space bar and left mouse buttonif(Input. GetKeyDown (KeyCode. Space) | | Input. GetKeyDown (KeyCode. Mouse0)) {/ / generates a bullet and launch GameObject shell = GameObject.Instantiate(shellPrefab, firePosition.position, firePosition.rotation) as GameObject; shell.GetComponent<Rigidbody>().velocity = shell.transform.forward * shellSpeed; }}}Copy the code
  1. Add the destroy script to the bullet, set the bullet as the trigger, and set the explosion effect to Play on Awake
using System.Collections; using System.Collections.Generic; using UnityEngine; Public class ShellDestroy: MonoBehaviour {public GameObject shellExplosion; Public void OnTriggerEnter(Collider Collider) {Instantiate(shellExplosion, transform.position, transform.rotation); // Destroy bullet itself Destroy(this.gameObject); }}Copy the code
  1. Add destruction script to explosion effect
using System.Collections; using System.Collections.Generic; using UnityEngine; Public class ExplosionDestroy: MonoBehaviour {// Disappear time publicfloatDestroyTime = 1.5 f; voidStart() { Destroy(this.gameObject, destroyTime); }}Copy the code
  1. Set the Tank label to Tank
  2. Modify bullet script
using System.Collections; using System.Collections.Generic; using UnityEngine; Public class ShellDestroy: MonoBehaviour {// Bullet explosion effect public GameObject shellExplosion; Public void OnTriggerEnter(Collider Collider) {// Deal damage to the tankif (collider.tag == "Tank")
		{
			collider.SendMessage("TakeDamage"); } // Instantiate gameObject.instantiate (shellExplosion, transform.position, transform.rotation); // Destroy bullet itself Destroy(this.gameObject); }}Copy the code
  1. Create a new health script for the tank, note that the explosion effect is set to play on Awake
using System.Collections; using System.Collections.Generic; using UnityEngine; Public class TankLife: MonoBehaviour {// Tank total health publicfloathp = 100; Public GameObject tankExplosion; // Damage function voidTakeDamage() {HP -= Random.Range(10, 20); / / explosionif(hp <= 0) { GameObject.Instantiate(tankExplosion, transform.position, transform.rotation); Destroy(this.gameObject); }}}Copy the code
  1. Duplicate a tank, change the color, name it Enemy, disable all control scripts except the life script
  2. Add a view-following script to the cameraInstead of scripting, move the camera behind and above the tank as a child of the turret
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowTank : MonoBehaviour {

	public Transform player;
	private Vector3 offset;

	// Use this for initialization
	void Start () {
		offset = transform.position - player.position;
	}

	void Update() { transform.position = player.position + offset; }}Copy the code