Unity游戏开发官方入门教程:飞机大战(九) | 您所在的位置:网站首页 › 飞机大战教程视频全集下载安装 › Unity游戏开发官方入门教程:飞机大战(九) |
Unity版本:Unity 2018.2.14f1 原视频链接:https://unity3d.com/cn/learn/tutorials/s/space-shooter-tutorial 教程目录: Unity游戏开发官方入门教程:飞机大战(一)——创建新项目、导入资源、设置场景 Unity游戏开发官方入门教程:飞机大战(二)——创建飞船对象 Unity游戏开发官方入门教程:飞机大战(三)——设置相机和光照 Unity游戏开发官方入门教程:飞机大战(四)——使用Quad加入背景 Unity游戏开发官方入门教程:飞机大战(五)——实现飞船控制脚本 Unity游戏开发官方入门教程:飞机大战(六)——创建子弹 Unity游戏开发官方入门教程:飞机大战(七)——发射子弹 Unity游戏开发官方入门教程:飞机大战(八)——创建销毁边界 Unity游戏开发官方入门教程:飞机大战(九)——创建和销毁敌人 Unity游戏开发官方入门教程:飞机大战(十)——敌人的爆炸和移动 Unity游戏开发官方入门教程:飞机大战(十一)——游戏控制 本节要点1.使用Random.insideUnitSphere控制陨石随机旋转 2.使用tag区分特殊的GameObject 3.使用OnTriggerEnter()函数来触发碰撞后GameObject的销毁 一、创建敌人1.Hierarchy中创建一个GameObject,名为Asteroid,重置transform,将Z设置为8. 2.在model中将prop_asteroid_01拖拽到我们的Asteroid上面,并重置transform: 1.Asteroid -> Add Component -> New Script -> 命名为RandomRotator,加入以下代码: using System.Collections; using System.Collections.Generic; using UnityEngine; public class RandomRotator : MonoBehaviour { public float tumble; private Rigidbody rb; void Start () { rb = GetComponent(); rb.angularVelocity = Random.insideUnitSphere * tumble; } }其中,Random.insideUnitSphere 用于返回一个随机的Vector3变量。 3.在Inspector中将tumble设置成5,查看随机翻转的效果: 1.当子弹碰撞到敌人,才会触发销毁,但Boundary碰撞到敌人不应该触发销毁,因此先使用Tag标记Boundary,以便在脚本中区分开来,具体加tag方法见下图: 2.Asteroid -> Add Component -> New Script -> 命名为DestroyByContact,加入以下代码: using System.Collections; using System.Collections.Generic; using UnityEngine; public class DestroyByContact : MonoBehaviour { //当其他碰撞器进入当前GameObject的触发器时,销毁该碰撞器对应的游戏对象,同时销毁该GameObject void OnTriggerEnter(Collider other) { if(other.tag == "Boundary") { return; } Destroy(other.gameObject); Destroy(gameObject); } }3.运行游戏,查看子弹攻击的销毁效果: 参考资料:https://unity3d.com/cn/learn/tutorials/s/space-shooter-tutorial |
CopyRight 2018-2019 实验室设备网 版权所有 |