Unity初级案例 - 坦克大战 Day 2

栏目: 编程语言 · 发布时间: 5年前

内容简介:1.空气墙的制作,标签的添加在Bullet添加完善碰撞检测2.玩家无敌方法与死亡方法的编写

1.空气墙的制作,标签的添加

在Bullet添加完善碰撞检测

private void OnTriggerEnter2D(Collider2D collision)
    {
        switch (collision.tag)
        {
            case "Tank":
                break;
            case "Heart":
                break;
            case "Enemy":
                break;
            case "Wall":
                break;
            case "Barriar":
                break;
            default:
                break;
        }

2.玩家无敌方法与死亡方法的编写

private bool isDefended = true;
    private float defendTimeVal = 3;
    public GameObject explosionPrefab;
    public GameObject defendEffectPrefab;

    //坦克的死亡方法
    private void Die()
    {
        if (isDefended)
        {
            return;
        }
        //产生爆炸特效
        Instantiate(explosionPrefab, transform.position, transform.rotation);
        //死亡
        Destroy(gameObject);
    }

    void Update () {
        //是否处于无敌状态
        if (isDefended)
        {
            defendEffectPrefab.SetActive(true);
            defendTimeVal -= Time.deltaTime;
            if(defendTimeVal <= 0)
            {
                isDefended = false;
                defendEffectPrefab.SetActive(false);
            }
        }
    }

3.区别玩家的子弹与敌人的子弹

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

public class Bullet : MonoBehaviour {

    public float moveSpeed = 10;

    public bool isPlayerBullet;

	// Update is called once per frame
	void Update () {
        transform.Translate(transform.up * moveSpeed * Time.deltaTime, Space.World);
	}

    private void OnTriggerEnter2D(Collider2D collision)
    {
        switch (collision.tag)
        {
            case "Tank":
                if (!isPlayerBullet)
                {
                    collision.SendMessage("Die");
                }              
                break;
            case "Heart":
                collision.SendMessage("Die");
                break;
            case "Enemy":
                break;
            case "Wall":
                Destroy(collision.gameObject);
                Destroy(gameObject);
                break;
            case "Barriar":
                Destroy(gameObject);
                break;
            default:
                break;
        }
    }
}

为坦克生成子弹的时候添加一条语句

GameObject bullet = Instantiate(bulletPrefab, transform.position, Quaternion.Euler(transform.eulerAngles + bulletEulerAngles));
bullet.GetComponent<Bullet>().isPlayerBullet = true;

4.修复bug,制作敌人的子弹

5.敌人的制作,玩家产生效果的添加

添加敌人Enemy的脚本,新增Born脚本用于玩家的初始生成

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

public class Born : MonoBehaviour {

    public GameObject playerPrefab;

	// Use this for initialization
	void Start () {
        Invoke("BornTank", 1f);
        Destroy(gameObject, 1f);
	}
	
	// Update is called once per frame
	void Update () {
		
	}

    private void BornTank()
    {
        Instantiate(playerPrefab, transform.position, Quaternion.identity);
    }
}

6.用同样的产生特效随机生成两种敌人

在Born里添加代码

public GameObject[] enemyPrefabList;

    public bool createPlayer;
    private void BornTank()
    {
        if (createPlayer)
        {
            Instantiate(playerPrefab, transform.position, Quaternion.identity);
        }
        else
        {
            int num = Random.Range(0, 2);
            Instantiate(enemyPrefabList[num], transform.position, Quaternion.identity);
        }
        
    }

7.敌人AI的编写

编写敌人自动攻击和移动的代码

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

public class Enemy : MonoBehaviour {

    //属性值
    public float moveSpeed = 3;
    private Vector3 bulletEulerAngles;
    private float v;
    private float h;
    
    //引用
    public Sprite[] tankSprites;//上 右 下 左
    public GameObject bulletPrefab;
    private SpriteRenderer sr;
    public GameObject explosionPrefab;

    //计时器
    private float time = 3f;
    private float timer = 0;
    private float timeValChangeDirection;


    private void Awake()
    {
        sr = GetComponent<SpriteRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        //攻击的时间间隔
        if (timer >= time)
        {
            Attack();

        }
        else
        {
            timer += Time.deltaTime;
        }
    }

    private void FixedUpdate()
    {
        Move();
    }

    //坦克的攻击方法
    private void Attack()
    {
        //子弹产生的角度:当前坦克的角度+子弹应该旋转的角度
        if (sr.sprite == tankSprites[2])
        {
            bulletEulerAngles = new Vector3(0, 0, -180);
        }
        else if (sr.sprite == tankSprites[3])
        {
            bulletEulerAngles = new Vector3(0, 0, 90);
        }
        else if (sr.sprite == tankSprites[1])
        {
            bulletEulerAngles = new Vector3(0, 0, -90);
        }
        else
        {
            bulletEulerAngles = new Vector3(0, 0, 0);
        }

        GameObject bullet = Instantiate(bulletPrefab, transform.position, Quaternion.Euler(transform.eulerAngles + bulletEulerAngles));
        bullet.GetComponent<Bullet>().isPlayerBullet = true;
        timer = 0;
    }

    //坦克的移动方法
    private 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.fixedDeltaTime, Space.World);
        if (v < 0)
        {
            sr.sprite = tankSprites[2];
            //bulletEulerAngles = new Vector3(0, 0, -180);
        }
        else if (v > 0)
        {
            sr.sprite = tankSprites[0];

        }

        if (v != 0)
        {
            return;
        }

        
        transform.Translate(Vector3.right * h * moveSpeed * Time.fixedDeltaTime, Space.World);
        if (h < 0)
        {
            sr.sprite = tankSprites[3];
            //bulletEulerAngles = new Vector3(0, 0, 90);
        }
        else if (h > 0)
        {
            sr.sprite = tankSprites[1];
            //bulletEulerAngles = new Vector3(0, 0, -90);
        }
    }

    //坦克的死亡方法
    private void Die()
    {
        //产生爆炸特效
        Instantiate(explosionPrefab, transform.position, transform.rotation);
        //死亡
        Destroy(gameObject);
    }
}

8.初始化地图 工具 的创建

创建MapCreation,添加创建老家的方法

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

public class MapCreation : MonoBehaviour {

    //用来装饰初始化地图所需物体的数组
    //0.老家 1.墙 2.障碍 3.出生效果 4.河流 5.草 6.空气墙
    public GameObject[] item;

    private void Awake()
    {
        //实例化老家
        CreateItem(item[0], new Vector3(0, -8, 0), Quaternion.identity);
        //用墙把老家围起来
        CreateItem(item[1], new Vector3(-1, -8, 0), Quaternion.identity);
        CreateItem(item[1], new Vector3(1, -8, 0), Quaternion.identity);
        for(int i = -1; i < 2; i++)
        {
            CreateItem(item[1], new Vector3(i, -7, 0), Quaternion.identity);
        }
    }

    private void CreateItem(GameObject createGameObject,Vector3 createPosition,Quaternion createRotation)
    {
        GameObject itemGo = Instantiate(createGameObject, createPosition, createRotation);
        itemGo.transform.SetParent(gameObject.transform);
    }
}

运行结果如下:

Unity初级案例 - 坦克大战 Day 2

9. 编写产生随机位置的方法

添加随机生成位置的方法,并且生成空气墙

//产生随机位置的方法
    private Vector3 CreateRandomPosition()
    {
        //不生产x=-10,10的两列,y=-8,8这两行的位置
        while (true)
        {
            Vector3 createPosition = new Vector3(Random.Range(-9, 10), Random.Range(-7, 8), 0);
            if (!HasThePosition(createPosition))
            {
                return createPosition;
            }         
        }
    }

    //用来判断位置列表中是否有这个位置
    private bool HasThePosition(Vector3 createPos)
    {
        for (int i = 0; i < itemPositionList.Count; i++)
        {
            if(createPos == itemPositionList[i])
            {
                return true;
            }
        }
        return false;
    }

    private void Awake()
    {
                //实例化外围墙
        for(int i = -11; i < 12; i++)
        {
            CreateItem(item[6], new Vector3(i, 9, 0), Quaternion.identity);
        }
        for (int i = -11; i < 12; i++)
        {
            CreateItem(item[6], new Vector3(i, -9, 0), Quaternion.identity);
        }
        for (int i = -8; i < 9; i++)
        {
            CreateItem(item[6], new Vector3(-11, i, 0), Quaternion.identity);
        }
        for (int i = -8; i < 9; i++)
        {
            CreateItem(item[6], new Vector3(11, i, 0), Quaternion.identity);
        }
    }

10.初始化地图其他的游戏物体

完善初始化地图

//产生敌人的方法
    private void CreateEnemy()
    {
        int num = Random.Range(0, 3);
        Vector3 EnemyPos = new Vector3();
        if(num == 0)
        {
            EnemyPos = new Vector3(-10, 8, 0);
        }
        else if (num == 1)
        {
            EnemyPos = new Vector3(0, 8, 0);
        }
        else                  
        {
            EnemyPos = new Vector3(10, 8, 0);
        }
        CreateItem(item[3], EnemyPos, Quaternion.identity);
    }


//初始化玩家
        GameObject go = Instantiate(item[3], new Vector3(-2, -8, 0), Quaternion.identity);
        go.GetComponent<Born>().createPlayer = true;

        //产生敌人
        CreateItem(item[3], new Vector3(-10, 8, 0), Quaternion.identity);
        CreateItem(item[3], new Vector3(0, 8, 0), Quaternion.identity);
        CreateItem(item[3], new Vector3(10, 8, 0), Quaternion.identity);

        InvokeRepeating("CreateEnemy", 4, 5);

        //实例化地图
        for (int i = 0; i < 60; i++)
        {
            CreateItem(item[1], CreateRandomPosition(), Quaternion.identity);
        }
        for (int i = 0; i < 20; i++)
        {
            CreateItem(item[2], CreateRandomPosition(), Quaternion.identity);
        }
        for (int i = 0; i < 20; i++)
        {
            CreateItem(item[4], CreateRandomPosition(), Quaternion.identity);
        }
        for (int i = 0; i < 20; i++)
        {
            CreateItem(item[5], CreateRandomPosition(), Quaternion.identity);
        }

运行结果如下:

Unity初级案例 - 坦克大战 Day 2


以上所述就是小编给大家介绍的《Unity初级案例 - 坦克大战 Day 2》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

MATLAB高效编程技巧与应用

MATLAB高效编程技巧与应用

吴鹏 / 北京航空航天大学 / 2010-6 / 39.00元

《MATLAB高效编程技巧与应用:25个案例分析》是作者八年MATLAB使用经验的总结,精心设计的所有案例均来自于国内各大MATLAB技术论坛网友的切身需求,其中不少案例涉及的内容和求解方法在国内现已出版的MATLAB书籍中鲜有介绍。 《MATLAB高效编程技巧与应用:25个案例分析》首先针对MATLAB新版本特有的一些编程思想、高效的编程方法、新技术进行了较为详细的讨论,在此基础上,以大量......一起来看看 《MATLAB高效编程技巧与应用》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具