unity3D final assignment, tank shooting game

unity3D final assignment, tank shooting game

Tank shooting game, can fire bullets to hit objects, tanks can break walls and trees, with background sound effects. Details are as follows: (download link at the end of the article)

Production process:

First, download and import the entire Kawaii Tank package from the Asset Store

Then, in order to ensure the normal operation of the materials in this package, we need to import the standard asset package at the same time

Finally, we import the test in Scenes in Kawaii Tank's package_ Field scene. Add some elements to the scene to make it our game map. (AI tanks are prone to make mistakes when moving on uneven ground. It is recommended to avoid entering uneven ground)

Design AI

This tank AI design mainly realizes two functions:

  1. Path planning
  2. Attack control

Path planning

The Nav Mesh Agent control provided by unity is mainly used for path planning

First, we add Nav Mesh Agent control on the main body component of each AI tank. There is no need to modify the settings. Just keep the default.

Then, we enter the Navigation page and click the Bake button to identify the whole map

Finally, we need to use scripts to implement tracking. Therefore, we create the following script and add it to the main body of the AI tank.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class PlaceTarget : MonoBehaviour
{
    float count;
    public GameObject target;  //Obtain the target point and pay attention to the assignment in the panel
    NavMeshAgent mr;   //Declare variable
                       // Use this for initialization
    void Start()
    {
        count = 0;
        //Get its own NavMeshAgent component
        mr = GetComponent<NavMeshAgent>();
    }

    // Update is called once per frame
    void Update()
    {
        count += Time.deltaTime;
        while (count > 1)
        {
            //Use attributes to transfer the coordinates of the target point
            //mr.destination = target.transform.position;
            //Use the method to obtain the coordinates of the target point, which is the same as the previous line of code
            mr.ResetPath();
            mr.SetDestination(target.transform.position);

            count = 0;
        }

    }
}

Note: the target attribute of each AI tank needs to be set manually. It is recommended to set it as the main component of the player tank to ensure normal tracking.

Then, in order to achieve simplicity, the tank will keep tracking the player after starting the game.

Attack control

For attack control, we use Collider, which is similar to the implementation of patrol operation. To do this, we need to modify tankai / main body / turret_ Base/Cannon_ Base component and its script Fire_Control_CS.cs

First, we set the tag of the main body of the Player tank object to Player

Then, we'll call cannon_ Add a Box Collider Collider to the base component (note that the column of Is Trigger needs to be checked), and set the collider as a rectangle to make it easier to identify the front and rear enemies

Then, modify the control script Fire_Control_CS.cs

...
void Update()
{
    if (idScript.isPlayer)
    {
        // When the object is a player
        #if UNITY_ANDROID || UNITY_IPHONE
            Mobile_Input ();
        #else
            Desktop_Input();
        #endif
    }
    else
    {
        // When the target is an AI tank, a bullet is fired every three seconds
        count = count + Time.deltaTime;
        if (count > 3.0f && canFire)
        {
            Fire();
            count = 0;
        }
    }
}

...
    
void OnTriggerEnter(Collider collider)
{
    // Turn on the fire switch when you recognize that the opponent is a player tank
    if (collider.gameObject.tag == "Player")
    {
        Debug.Log("catch");
        target = collider.gameObject.transform;
        canFire = true;
    }
}
....

Finally, attach the script to the Cannon Base component of AI tank to realize attack control.

PS: in order to reduce the difficulty, the AI tanks here can only fire bullets directly in front of them, but it's hard enough to play.

In addition to the above contents, I also refer to the elder martial brother's blog to realize the operations such as the disappearance of the tank after the blood volume reaches 0, but because it is not the key point, I won't describe it more here.

Resource download link:

https://download.csdn.net/download/weixin_43474701/20306200

Keywords: Unity

Added by chrille112 on Mon, 17 Jan 2022 18:16:58 +0200