Unity飞机大战(1) 玩家飞机移动控制和子弹发射+生成陨石和敌机

Unity制作飞机大战第一步

1.把资源包导入Unity并搭建好场景
2.并把做好的预制体 拖入一个新的文件夹
如下图:
Unity飞机大战(1) 玩家飞机移动控制和子弹发射+生成陨石和敌机_第1张图片

飞机控制脚本,此脚本挂在玩家飞机上

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

public class PlayerShipCtr : MonoBehaviour
{

    public float MoveSpeed;
    public float FireTime;
    public GameObject PlayerBullet;
   

    //私有变量同意使用_下划线
    private Rigidbody _rig;
    private AudioManagerCtr _audioManager;
    private float _timer;   // 计时器
    private GameObject _obj;
    private CreateGameManagerCtr _createGameManagerCtr;


    
    void Start()
    {
        
        _obj = transform.Find("Protect").gameObject;
        FireTime = 0.25f;
        _rig = GetComponent<Rigidbody>();
        if (null == _rig)
        {
            _rig = gameObject.AddComponent<Rigidbody>();
        }
        _rig.useGravity = false;
        if (MoveSpeed == 0)
        {
            MoveSpeed = 5;
        }
        //找到标签的对象 并得到其组件
        _audioManager = GameObject.FindWithTag("AudioManager").GetComponent<AudioManagerCtr>();
        _createGameManagerCtr = GameObject.FindWithTag("CreateGameManager").GetComponent<CreateGameManagerCtr>();
        _timer = 0;

    }

    //逻辑
    void Update()
    {
       if(_createGameManagerCtr.PlayerIsProtected)
        {
            _obj.SetActive(true);
        }else
        {
            _obj.SetActive(false);
        }
        _timer += Time.deltaTime;
        if (Input.GetMouseButton(0) && _timer >= FireTime)  //用于判断发射的时间间隔
        {
            //实例化子弹
            _timer = 0;
      GameObject obj =      Instantiate(PlayerBullet);
            obj.transform.position = gameObject.transform.Find("BulletPos").position;
            if(obj.tag.Equals(TagManager.PlayBulletName)) // 1发
            obj.GetComponent<PlayerBulletCtr>().MoveSpeed = 7;
            
                 _audioManager.PlayAudio(1,AudioName.PlyerShoot);
        }
    }

    //物理
    void FixedUpdate()  //每个固定的物理时间 用于物理引擎的参数更新
    {
        //edit->Project settings->Input  设置
        float _v = Input.GetAxisRaw("Vertical");  //得到垂直轴向输入
        float _h = Input.GetAxisRaw("Horizontal");  //得到水平轴向的输入
        
        Vector3 v3 = new Vector3(_h, 0, _v).normalized; //向量归一化 一般用在控制移动 获取方向
        _rig.velocity = v3 * MoveSpeed;

        //飞机在屏幕中移动的范围
        _rig.transform.position = new Vector3(
            Mathf.Clamp(_rig.transform.position.x, -6.8f, 6.8f),
            0,
            Mathf.Clamp(_rig.transform.position.z, -3.5f, 14));

        //左右移动时  让飞机有一个30°的倾斜
        _rig.transform.rotation = Quaternion.AngleAxis(30 * _h, transform.forward);
    }
}

玩家子弹的脚本,此脚本挂在预制体PlayerBullet上

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

public class PlayerBulletCtr : MonoBehaviour {

	// Use this for initialization
    public float MoveSpeed;

    private Rigidbody _rig;
	void Start ()
	{
     //   transform.rotation = Quaternion.Euler(90,0,0);
	    _rig = GetComponent<Rigidbody>();
	    if (null == _rig)
	    {
	        _rig = gameObject.AddComponent<Rigidbody>();
	    }
	    _rig.useGravity = false;  //子弹重力为false
        if(MoveSpeed == 0)
	    MoveSpeed = 5;  //如果为0  就默认子弹速度为5

	    _rig.velocity = transform.up * MoveSpeed;
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

生成陨石和敌机脚本,此脚本挂在CreateGameManager上

建一个空物体 改名为CreateGameManager,用于控制陨石的生成和敌机的生成

Unity飞机大战(1) 玩家飞机移动控制和子弹发射+生成陨石和敌机_第2张图片


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

public class CreateGameManagerCtr : MonoBehaviour {

    //x -->  -3.5  -2  -0.5  1  2.5  3.5
    // 0
    // 16 
    //0 敌机
    // 1~3 陨石
    //波次 协同
    //波次 数量
    public GameObject[] EnemyObjects; //敌人的数组(包括陨石和敌机)
     //爆炸特效 
    public GameObject[] ParitcleObjects;  //爆炸特效的数组
    public GameObject Jack;  //拖入预制体玩家子弹
    public GameObject[] Bolls; //public一个球的数组  吃了球有特殊功能
    public bool PlayerOver;
    public bool CellPlayerIsProtected;
    public bool PlayerIsProtected;
    //存储初始化点的位置
    private Vector3[] CreatePos;
    private BollCtr _bollctr;
    private float _timer;
    private PlayerShipCtr _playerShipCtr;
	void Start () {
        _bollctr = GameObject.FindWithTag("BlueBoll").GetComponent<BollCtr>();
        _playerShipCtr = GameObject.FindWithTag(TagManager.PlayName).GetComponent<PlayerShipCtr>();
        CellPlayerIsProtected = true;
	    PlayerIsProtected = true;
	    PlayerOver = false;
        CreatePos = new Vector3[8];  //随机八个位置 让陨石从这些位置生成
	    for (int i = 0; i < 7; i++)
	    {
            //
	        CreatePos[i].Set(-6.5f+i*1.8f, 0, 16);
	    }
        CreatePos[CreatePos.Length-1].Set(6.5f,0,16);

	    StartCoroutine("IEnumCreateEnemy");  //开始协同
	}
	
	// Update is called once per frame
	void Update ()
	{
	    _timer += Time.deltaTime;
	    if (CellPlayerIsProtected)
	    {
	        PlayerIsProtected = true;
	        CellPlayerIsProtected = false;
	        _timer = 0;
	    }
	    if (_timer >= 5.0f)
	    {
	        PlayerIsProtected = false;
	    }
        if (_bollctr.ISOBJ)
        {
            StartCoroutine("HEIHEI");  //开始协同
        }

    }

    IEnumerator IEnumCreateEnemy()
    {
        int EnemyShipCount = 0;
        while (true)
        {
            EnemyShipCount = 0;
            int tmpNumber = Random.Range(8, 15);  //随机陨石的数量8~15
            for (int i = 0; i < tmpNumber; i++) //不管一波生成几个陨石  反正定义顺序三个敌人出现一个敌机
            {
                EnemyShipCount++;
                CreateEnemy(EnemyShipCount); //调用下面CreateEnemy方法
                yield return  new WaitForSeconds(0.4f);//陨石一个接一个出现 间隔为0.4s
            }
            yield return new WaitForSeconds(1.5f);//每一波陨石出现的时间间隔为1.5s
        }
    }
    //此协同是为了  玩家吃了蓝色球有特殊子弹  5s钟后特殊子弹消失
    IEnumerator HEIHEI()
    {
            yield return new WaitForSeconds(5.0f);   //5s钟后变回原来的子弹
            _playerShipCtr.PlayerBullet = Jack;

        
    }


    public void CreateEnemy(int shipCount)
    {
        GameObject tmpGameObject;
        if (shipCount % 3 == 0)   //如果敌人的数量除以3余0的话 实例化一个敌机
        {
            //敌机
           tmpGameObject =  Instantiate(EnemyObjects[0], CreatePos[Random.Range(0, CreatePos.Length)], Quaternion.identity);
            tmpGameObject.GetComponent<EnemyShipCtr>().Score = Random.Range(10, 20);
            tmpGameObject.GetComponent<EnemyShipCtr>().Left = Random.Range(0, 3);
           
        }
        else
        {
            //陨石
            tmpGameObject = Instantiate(EnemyObjects[Random.Range(1,EnemyObjects.Length)], CreatePos[Random.Range(0, CreatePos.Length)], Quaternion.identity);
            tmpGameObject.GetComponent<AsteroidCtr>().Score = Random.Range(5, 10);

        }
        if (shipCount % 4 == 0)   //每第四个敌人死亡 生成一个随机球
        {
            if (tmpGameObject.tag.Equals(TagManager.EnemyShip)) //如果碰撞到的是敌机
            {
            //实例化随机化球
                tmpGameObject.GetComponent<EnemyShipCtr>().Boll = Bolls[Random.Range(0, Bolls.Length)];
            }
            else
            {
                tmpGameObject.GetComponent<AsteroidCtr>().Boll = Bolls[Random.Range(0, Bolls.Length)];
                
            }
        }
           

    }

    public void CreateParticleByV3AndIndex(Vector3 CreatePoint, int index)
    {
        //实例化例子爆炸特效
        GameObject tmpGameObject = Instantiate(ParitcleObjects[index], CreatePoint, Quaternion.identity);
        Destroy(tmpGameObject,2f);   //两秒后销毁特效
    }

    public void StopGame()
    {
        PlayerOver = true;
        StopCoroutine("IEnumCreateEnemy");  //停止协同

    }
}

你可能感兴趣的:(Unity)