2.5 unity实战 飞机大战

                                                                     
Unity飞机大战
一,场景布置
1,模拟飞行场景,
做一个地图的循环轮播,这里需要两张地图进行循环轮播,这样视觉不容易看出变化.
a,设置天空盒: Window-lighting-Setting-skybox material选择
b,灯光调控(有时候图片背景较暗,物体在上面显示有些看不清楚,这时就需要调控灯光了)
点击Directional Light -更改Mode-选择realtime
c,控制地图轮播(在外面调控地图播放速度也可以内部直接赋值)

public class BackGroundScroll : MonoBehaviour {
float bgSpeed=2f; //背景的速度
float bgMaxSize=30f; //背景移动的最大距离
Vector3 bgStartPosition; //初始位置
private void Start()
{
bgStartPosition = transform.position; //将物体的位置作为初始位置
}
private void Update()
{
//设置一个位移距离,使用Mathf.Repeat()重复生成
//函数Mathf.Repeat(t : float, length : float),循环数值t,0到length之间。
//t值永远不会大于length的值,也永远不会小于0。
//数值从0+t一直加到length.循环重复.如果t为负数,length不断减少,减到0,重复.
float bgNewPositiopn = Mathf.Repeat(-Time.time * bgSpeed, bgMaxSize);
//相对位移,位移方向就是z轴,不断赋值Position.
transform.position = bgStartPosition + bgNewPositiopn * Vector3.forward;
}
}

二,设置玩家飞机
1,飞机位移以及相关知识点
a,飞机位移
float playerSpeed=6.4f; //飞机速度
Rigidbody playerRig; //定义一个刚体

playerRig = gameObject.GetComponent<Rigidbody>(); //获得刚体

float hor = Input.GetAxis("Horizontal"); //获得水平位置
float ver = Input.GetAxis("Vertical"); //获得垂直位置
Vector3 pos = new Vector3(hor,0,ver); //获取按键输入向量
playerRig.velocity = pos * playerSpeed; //得到飞机飞行大小与方向,飞机飞行

b,限制边界
[System.Serializable]
public class Bound
{
public float xMin, xMax, zMin, zMax;
} //系统序列化

playerRig.position = new Vector3(
Mathf.Clamp(playerRig.position.x,bound.xMin,bound.xMax),
0,
Mathf.Clamp(playerRig.position.z,bound.zMin,bound.zMax)
);
//限制飞机飞行边界
//函数Mathf.Clamp(value : float, min : float, max : float)
//value:所要限制的量 , min:最小值 ,max:最大值

c,飞行特效

float angle=-3f; //定义旋转角度
playerRig.rotation = Quaternion.Euler(0, 0, angle * playerRig.velocity.x);
//模拟特效,当飞机飞的时候,左右摇摆.

2,飞机喷气
直接拖入喷气即可
3,飞机开火(这里代码较多)设置左右两边点击鼠标发射,中间按空格
[System.Serializable]
public class FirePoint
{
public Transform firePoint1; //开火位置1
public Transform firePoint2; //开火位置2
public Transform firePoint3; //开火位置3
}
public class PlayerFire : MonoBehaviour {
public GameObject bullet; //子弹
public Transform bulletParent; //管理子弹
public FirePoint firePoint; //获取开火位置
float shootRate = 0.25f; //射击频率,0.25秒一发
float timer; //管理时间
void Update () {
timer += Time.deltaTime; //开启定时器
//判断是否点击鼠标右键以及时间是否超过时间间隔
if (Input.GetButton("Fire1") && timer>shootRate)
{
timer = 0; //时间清零
GameObject go1 = Instantiate(bullet,firePoint.firePoint2.position,Quaternion.identity); //生成飞机左边子弹
GameObject go2 = Instantiate(bullet, firePoint.firePoint3.position, Quaternion.identity); //生成飞机右边子弹
go1.transform.parent = bulletParent;
go2.transform.parent = bulletParent;
}
if (Input.GetKey(KeyCode.Space))
{
GameObject go = Instantiate(bullet, firePoint.firePoint1.position, Quaternion.identity); //生成飞机中间子弹
go.transform.parent = bulletParent;
}
}
}

三,设置子弹
1,子弹移动以及销毁(注意,子弹速度需要比飞机快)
public class BulletMove : MonoBehaviour {
void Start () {
GetComponent<Rigidbody>().velocity = transform.forward * 7f;
}
private void FixedUpdate()
{
if (transform.position.z>18f)
{
Destroy(gameObject);
}
}
}

四,设置敌人
1,敌机生成
public class EnemyCreat : MonoBehaviour {
public GameObject[] enemys; //导入物体
Vector3 creatLoction = new Vector3(6.82f,0,8.83f); //获得生成位置最大值
void Start () {
//InvokeRepeating (methodName : string, time : float, repeatRate : float) :
//methodName:方法名 ,time:生成时间,repeatRate:生成间隔
InvokeRepeating("CreateEnemy", 1f, 3f);
}
void CreateEnemy()
{
//随机生成数量
for (int i = 0; i < Random.Range(1,3); i++)
{
//1,获取敌机类型
GameObject enemy = enemys[Random.Range(0, enemys.Length)];
//2,随机位置
Vector3 location = new Vector3(
Random.Range(-creatLoction.x, creatLoction.x),
creatLoction.y,
creatLoction.z
);
//3,生成
GameObject go = Instantiate(enemy,location,Quaternion.Euler(0,180,0));
//4.指定父类
go.transform.parent = transform;
}
}
}
2,敌机移动(同上)
3,敌机攻击(同上)

五,碰撞检测(爆炸):
1,玩家碰撞检测

public class PlayerExp : MonoBehaviour {
public GameObject playerExp;
private GameObject Exp;
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player" || other.tag == "PlayerBullet") return;
if (other.tag == "EnemyBullet")
{
Exp = Instantiate(playerExp, transform.position, transform.rotation);
}
Destroy(gameObject);
Destroy(other.gameObject);
Destroy(Exp, 2f);
}
}

2,敌机碰撞检测
public class EnemyExp : MonoBehaviour {
public GameObject enemyExp;
public GameObject playerExp;
private GameObject Exp;
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Enemy" || other.tag == "EnemyBullet") return;
if (other.tag == "PlayerBullet")
{
Exp = Instantiate(enemyExp, transform.position, transform.rotation);
}
if (other.tag == "Player")
{
Exp = Instantiate(playerExp, transform.position, transform.rotation);
}
Destroy(gameObject);
Destroy(other.gameObject);
Destroy(Exp, 2f);
}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值