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

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 简单很多!

Published by

风君子

独自遨游何稽首 揭天掀地慰生平