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

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

放上完成后的图:

给飞机设置的组件:

子弹的组件:

陨石的组件:

敌机和其子弹的组件与飞机类似,就不放图了~

接下来是代码:
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 简单很多!

Unity学习笔记:Unity 3D 飞机大战相关推荐

  1. Unity学习笔记——Unity基础一:unity界面、场景、游戏物体、组件等基本概念

    一.unity界面 如下图,unity最主要的界面为Scene(场景视图).Hierarchy(层级视图).project(工程视图).Inspector(检视视图) project--工程视图 当前 ...

  2. unity学习笔记——unity组件的从属关系

    unity项目组建:项目--场景--游戏对象--组件--属性 (unity是面向组件开发游戏的) 以下这张图,就很好地总结了unity中各组件之间的从属关系: 下面这张里都是一些比较常见的unity组 ...

  3. unity学习笔记-番外(3d模型的动作设计以及导入-2018版)材质的替换以及动作穿模(自己的手穿模到自己的其他部位)

    unity学习笔记-番外(3d模型的动作设计以及导入) 动作设计白嫖方法 方法一:小k网 需要注意的地方 方法二:mixamo 需要注意的地方 材质的替换 一 动作的穿模 2021.5.13更新 -2 ...

  4. Unity学习笔记1 简易2D横版RPG游戏制作(一)

    这个教程是参考一个YouTube上面的教程做的,原作者的教程做得比较简单,我先参考着做一遍,毕竟我也只是个初学者,还没办法完全自制哈哈.不过我之前也看过一个2D平台游戏的系列教程了,以后会整合起来,做 ...

  5. Unity学习笔记:个人学习项目《疯狂天才埃德加》纠错文档

    Unity学习笔记:个人学习项目<疯狂天才埃德加>纠错文档 本文档是完成学校Unity课程作业时建立的纠错文档.用于记录自己开发过程中遇到的各种问题,以便下次遇到相同的问题时及时找到解决方 ...

  6. 【Unity学习笔记】b站Unity架构课Unity3D 商业化的网络游戏架构(高级/主程级别)

    [Unity学习笔记]b站Unity架构课Unity3D 商业化的网络游戏架构(高级/主程级别) 自己跟着学完了,写了不少代码,会放在CSDN代码库,因为老师并没有提供源码,录屏也不是完全连续,所以难 ...

  7. Unity学习笔记(4)-----粒子效果的实现

    Unity学习笔记(4)-–粒子效果的实现 一.效果展示 下面用若干张张动图展示效果: 大概就是这样,并不是很难. 实际效果要比图中的好一点(顺畅得多). 实现步骤 大致可以分为如下几个步骤,然后逐个 ...

  8. 【Unity学习笔记】[Unity中文课堂教程] C#中级编程代码

    [Unity学习笔记][Unity中文课堂教程] C#中级编程代码 最近想补一补C#基础,Unity官方的C#中级编程教程质量很高,于是开个帖子把跟着敲+记录了部分价讲解和我自己的理解的代码存在这 原 ...

  9. 【Unity学习笔记】UnrealToUnity教程:(网上购买的素材导入Unreal+插件转Unity)

    [Unity学习笔记]UnrealToUnity教程: 最近想从Unreal那边化点缘借借素材,没想到踩到一个大坑 一,素材导入Unreal 这个教程比较多,根据素材的来源,传送门是以下这几个: 1. ...

最新文章

  1. xml file too big to import to wordpress website
  2. 使用 openssl反弹加密 shell
  3. @@ROWCOUNT 含义
  4. 【S操作】冰箱正常运行监控系统需求整理
  5. CreateFileMapping 内存映射读写文件
  6. Java 中的悲观锁、乐观锁、自旋锁、适应性自旋锁、偏向锁、轻量级锁、重量级锁、公平锁、非公平锁、可重入锁、共享锁等
  7. uuid生成_php如何生成 uuid(总结)
  8. 新手程序员如何找一个靠谱的公司
  9. 计算机控制炉温实验,计算机控制(炉温控制)实验报告-20210412070439.docx-原创力文档...
  10. iOS开发多线程篇—线程间的通信
  11. java 默认为空的注解,错误注解的字段设置一个默认的空值
  12. JSON对象和JSON数组
  13. [转载]注册电气工程师(供配电)执业资格考试基础考试大纲
  14. html标签选择器只认最后一个,选择某类的最后一个元素——CSS3伪类选择器走过的坑...
  15. 在phpstudy中安装并使用ThinkPHP 5
  16. linux刻录光盘空间不足,Linux下的光盘刻录技巧
  17. 携手推进国产化发展,未来智安与麒麟软件完成兼容互认证
  18. Python面试——基础面试题
  19. java字符串和字符数组相互转化的方法
  20. 打卡小程序上线啦!新玩法来了!

热门文章

  1. 容器化技术与微服务结合---结合springcloud微服务框架进行部署(含切换成阿里云docker仓库)(五)
  2. crypto-dirty laundry(zer0pts CTF 2020)
  3. 双十一十周年以后 电商行业这些未来趋势和机会不容错过
  4. 基于FPGA的DDS参考设计
  5. CUDA编程(一):GPU计算与CUDA编程简介
  6. 走进“开源SDR实验室” 一起玩转GNU Radio:gr-analog
  7. 【Linux】fork之后,子进程继承了父进程哪些内容
  8. 宁波区块链联盟正式成立 inTouch社交能否成为区块链领域的又一匹黑马?
  9. antd Modal
  10. HTTP请求中POST与GET的区别