Unity学习笔记:Unity 3D 飞机大战

版权声明:本文纯属原创,未经同意请勿转载。 https://blog.csdn.net/beiluo77/article/details/88062915

Unity学习笔记:Unity 3D 飞机大战

1、打开unity软件后,首先新建Quad作为背景,导入飞机模型,并为其添加刚体
然后创建C#脚本,挂载到飞机上。
2、给飞机创建子弹,让子弹成为预制体,同样创建C#脚本
3、创建陨石和敌机作为敌方,飞机发射子弹使其销毁,如果飞机与敌方相撞,则飞机爆炸消失。

放上完成后的图:
在这里插入图片描述
给飞机设置的组件:
alt
子弹的组件:
在这里插入图片描述
陨石的组件:
在这里插入图片描述
敌机和其子弹的组件与飞机类似,就不放图了~

接下来是代码:
1、飞机脚本

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


/** 玩家飞机飞行的脚本 */
public class PlayerMovement : MonoBehaviour
{
    /** 飞机可以飞行的区域 */
    public float xMax;
    public float xMin;
    public float zMax;
    public float zMin;

    /** 飞机自身的刚体 以及 飞机发射子弹的位置 */
    Rigidbody _plane_rig;           
    Transform _plane_fire_point;

    /** 子弹的预制体 */
    GameObject _bullet;

    private void Awake()
    {
        //获取刚体
        _plane_rig = this.GetComponent<Rigidbody>();
        //获取开火位置
        _plane_fire_point = transform.GetChild(1);
        //获取子弹的预制体
        //通过资源加载的方式获取预制体
        _bullet = Resources.Load("Prefabs/Bullet") as GameObject;
    }

    private void FixedUpdate()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        //飞机移动
        Move(h, v);
        //飞机开火
        Shoot();
    }

    /** 飞机的飞行速度 以及飞机的倾斜度 */
    public float plane_speed = 3f;
    public float plane_Tilt = 2f;
    void Move(float h,float v)
    {
        //1.获取飞机移动的方向
        Vector3 plane_fly_dir = new Vector3(h, 0, v);
        //2.设定飞机的飞行速度
        _plane_rig.velocity = plane_fly_dir * plane_speed;
        //3.移动
        _plane_rig.position = new Vector3
            (
                //Mathf.Clamp(x,y,z)的作用是将x限定在y和z之间
                Mathf.Clamp(_plane_rig.position.x,xMin,xMax),
                5f,
                Mathf.Clamp(_plane_rig.position.z,zMin,zMax)
            );
        //4.设置飞机倾斜度
        _plane_rig.rotation = Quaternion.Euler(0f, 0f, _plane_rig.velocity.x * -plane_Tilt);
    }


    float _shoot_rate = 0.5f;
    float _timer = 0f;
    void Shoot()
    {
        //计时
        _timer += Time.deltaTime;
        //按下鼠标左键
        if (Input.GetMouseButton(0) && _timer >= _shoot_rate)
        {
            //在_plane_fire_point.position的位置生成_bullet实例,保持_plane_fire_point.rotation的旋转
            Instantiate(_bullet, _plane_fire_point.position, _plane_fire_point.rotation);
            //重置定时器
            _timer = 0f;
        }
    }
}

2、子弹的脚本

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

public class BulletMovement : MonoBehaviour
{
    /** 子弹的飞行速度 */
    public float bullet_speed = 20f;

    private void FixedUpdate()
    {
        this.GetComponent<Rigidbody>().velocity = bullet_speed * transform.forward;
    }
}

3、陨石的脚本

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

public class AsteroidMove : MonoBehaviour
{
    //陨石的移动速度集合
    float[] _asteroid_speeds = { 8, 10, 13, 16 };

    private void Awake()
    {
        //左闭右开 [0,4)
        int index = Random.Range(0, _asteroid_speeds.Length);

        this.GetComponent<Rigidbody>().velocity = Vector3.back * _asteroid_speeds[index];
    }

}

陨石旋转

 //陨石旋转的速率集合
    float[] rotRations = { 4f, 6f, 8f };

    private void Awake()
    {
        int index = Random.Range(0, rotRations.Length);

        //Random.insideUnitSphere 随机返回球体(半径为1的球体)内部的任意一点
        this.GetComponent<Rigidbody>().angularVelocity = Random.insideUnitSphere * rotRations[index];
    }

陨石爆炸

/** 爆炸效果预制体 */
    public GameObject asteroidExp;
    public GameObject playerExp;

    private void OnTriggerEnter(Collider other)
    {
        //碰到陨石或者是敌方飞机 不处理直接返回
        if (other.gameObject.tag == "Asteroid" || other.gameObject.tag == "Enemy" || other.gameObject.tag == "EBullet")
            return;

        //碰到了子弹
        if(other.gameObject.tag == "Bullet")
        {
            //生成爆炸体
            GameObject exp = Instantiate(asteroidExp, transform.position, transform.rotation);
            //销毁
            Destroy(exp, 0.3f);
        }

        //碰到了玩家
        if(other.gameObject.tag == "Player")
        {
            //生成爆炸体
            GameObject exp = Instantiate(playerExp, transform.position, transform.rotation);
            //销毁
            Destroy(exp, 0.3f);
        }

        //销毁碰到的物体
        Destroy(other.gameObject);
        //销毁自身
        Destroy(transform.gameObject);

    }

以及陨石的管理

 /** 三个陨石预制体 */
    GameObject _Asteroid_01;
    GameObject _Asteroid_02;
    GameObject _Asteroid_03;

    /** 存放所有的陨石 */
    List<GameObject> _Asteroids;

    private void Awake()
    {
        //初始化陨石集合
        _Asteroids = new List<GameObject>();

        //通过资源加载的方式先获取到三个陨石
        _Asteroid_01 = Resources.Load("Prefabs/Asteroid_01") as GameObject;
        _Asteroid_02 = Resources.Load("Prefabs/Asteroid_02") as GameObject;
        _Asteroid_03 = Resources.Load("Prefabs/Asteroid_03") as GameObject;

        //将三个陨石加入到集合中
        _Asteroids.Add(_Asteroid_01);
        _Asteroids.Add(_Asteroid_02);
        _Asteroids.Add(_Asteroid_03);
    }

    private void Start()
    {
        //重复调用某一个方法
        InvokeRepeating("CreateAsteroid", 0f, 3f);
    }

    void CreateAsteroid()
    {
        //1.随机从集合中取出陨石
        GameObject asteroid = _Asteroids[Random.Range(0, _Asteroids.Count)];
        //2.创建随机位置
        Vector3 asteroidPos = 
            new Vector3(Random.Range(transform.position.x, -transform.position.x), 5f, 17.52f);
        //3.创建陨石
        Instantiate(asteroid, asteroidPos, Quaternion.identity);
    }

    private void OnApplicationQuit()
    {
        //当程序结束的时候取消invoke的重复调用
        CancelInvoke("CreateAsteroid");
    }

4、最后是敌机和其子弹

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

public class EnemyShip : MonoBehaviour {

    //敌机的移动速度集合
    float _enemy_speeds = 3f;
    Transform _eplane_fire;

    /** 敌机子弹的预制体 */
    GameObject _ebullet;

    private void Awake()
    {
        //左闭右开 [0,4)
        this.GetComponent<Rigidbody>().velocity = Vector3.back * _enemy_speeds;
        transform.eulerAngles = new Vector3(transform.eulerAngles.x, 180, transform.eulerAngles.z);

        //获取开火位置
        _eplane_fire = transform.GetChild(1);
        //获取子弹的预制体
        //通过资源加载的方式获取预制体
        _ebullet = Resources.Load("Prefabs/EBullet") as GameObject;

    }

    private void FixedUpdate()
    {
        //敌机开火
        Shoot();
    }

    float _shoot_rate = 2f;
    float _timer = 0f;
    void Shoot()
    {
        //计时
        _timer += Time.deltaTime;
        if (_timer >= _shoot_rate)
        {
            //在_plane_fire_point.position的位置生成_bullet实例,保持_plane_fire_point.rotation的旋转
            Instantiate(_ebullet, _eplane_fire.position, Quaternion.identity);
            //重置定时器
            _timer = 0f;
        }
    }
}

(敌机的爆炸及其子弹的爆炸和陨石爆炸代码类似,下面就不放了)


5、最后的最后(终于要完成了~)给敌机设置管理

/** 敌机 */
    GameObject _enemys;

    private void Awake()
    {
        //通过资源加载的方式先获取到敌机
        _enemys = Resources.Load("Prefabs/EnemyShip") as GameObject;
    }

    private void Start()
    {
        //重复调用某一个方法
        InvokeRepeating("CreateEnemy", 0f, 3f);
    }

    void CreateEnemy()
    {
        
        //1.创建随机位置
        Vector3 enemyPos =
            new Vector3(Random.Range(transform.position.x, -transform.position.x), 5f, 17.52f);
        //2.创建陨石
        Instantiate(_enemys, enemyPos, Quaternion.identity);
    }

    private void OnApplicationQuit()
    {
        //当程序结束的时候取消invoke的重复调用
        CancelInvoke("CreateEnemy");
    }

撒花!!!一个飞机大战小游戏就完成了,用 unity 做这款小游戏有些大材小用,却也的确比用 h5 简单很多!

猜你喜欢

转载自blog.csdn.net/beiluo77/article/details/88062915