我是靠谱客的博主 敏感电话,最近开发中收集的这篇文章主要介绍Unity3D 太空大战——整理,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Ok、now、begin!


今天同一整理学习了太空大战的开发、现在来做个总结:


1、首先是背景的生成和管理:

    星空   直接建一张plane,再贴上星星的图片。

火星  mars.png   (生成一个Material),  设置  shader —>  Transparent—>cutout—>diffuse

建好之后保存场景,计作为Level0,(也就是关卡1)

2、开始创建主角飞机,

它有这么几个属性:

移动速度:m_speed = 1,

生命值:  m_life = 10  ,   

前后左右方向的移动:movev = 0 ,  moveh = 0

再定义了一个子弹的附件  Transform m_rocket  (用来发射子弹,计:一定要在子弹对象实例化后,关联文件)

飞机的移动:   this.transform.Translate(moveh,0,movev);

发射子弹:  Instantiate(m_rocket,m_tranform.position,m_transform.ratation)

主角Player的代码:

using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public Transform m_rocket;
public float m_speed = 5;
public float rocketLaunchTime = 0;
public float m_life = 10;
public AudioClip m_shootClip;
public AudioSource m_audio;
public Transform m_explosionFX;
// Use this for initialization
void Start () {
m_audio = this.audio;
}
// Update is called once per frame
void Update () {
float movev = 0;
float moveh = 0;
if (Input.GetKey (KeyCode.W)) {
movev = -m_speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.S)) {
movev = m_speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.A)) {
moveh = m_speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.D)) {
moveh = -m_speed * Time.deltaTime;
}
rocketLaunchTime = rocketLaunchTime - Time.deltaTime;
if (rocketLaunchTime <= 0) {
rocketLaunchTime = 0.1f;
if (Input.GetKey (KeyCode.Space) || Input.GetMouseButton (0)) {
Instantiate(m_rocket,this.transform.position,this.transform.rotation);
m_audio.PlayOneShot(m_shootClip);
}
}
/*
if (Input.GetKey (KeyCode.Space) || Input.GetMouseButton (0)) {
Instantiate(m_rocket,this.transform.position,this.transform.rotation);
}
*/
this.transform.Translate (new Vector3(moveh,0,movev));
if(m_life <= 0){
Destroy(this.gameObject);
}
}
/*
void OnGUI(){
GUILayout.Label ("blood:
"+m_life);
}
*/
void OnTriggerEnter(Collider other){
if (other.tag.CompareTo ("Enemy") == 0) {
//m_life = m_life - 2;
Enemy enemy = other.GetComponent<Enemy>();
if(enemy != null){
m_life = m_life - enemy.m_power;
}
}
if (other.tag.CompareTo ("SuperEnemy") == 0) {
SuperEnemy superenemy = other.GetComponent<SuperEnemy>();
if(superenemy != null){
m_life = m_life - superenemy.m_power2;
}
}
if (other.tag.CompareTo ("EnemyRocket") == 0) {
EnemyRocket erocket = other.GetComponent<EnemyRocket>();
if(erocket != null){
//Destroy(this.gameObject);
m_life = m_life - 2;
}
}
//飞机毁灭
if(m_life <= 0){
Instantiate(m_explosionFX,this.transform.position,Quaternion.identity);
Destroy(this.gameObject);
}
}
}



3、创建子弹对象

先将相应的的模型拖入其中,

属性:    

移动速度: m_speed = 10,

生命周期: m_liveTime = 1;

伤害力: m_power = 1;

子弹demo:

using UnityEngine;
using System.Collections;
public class Rocket : MonoBehaviour {
public float m_speed = 10;
public float m_liveTime = 1;
public float m_power = 2;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
m_liveTime = m_liveTime - Time.deltaTime;
if (m_liveTime <= 0) {
Destroy(this.gameObject);
}
this.transform.Translate (new Vector3(0,0, -m_speed * Time.deltaTime));
}
void OnTriggerEnter(Collider other){
if (other.tag.CompareTo ("Enemy") == 0) {
Destroy(this.gameObject);
}
}
}

4、现在要开始创建子弹的Prefab了、

5、创建敌人Enemy的对象:

属性:

敌人移动速度: m_speed = 1;

旋转角度: m_rotspeed = 30;

旋转方向变换时间: m_timer = 1.5f;

生命条: m_life = 10;

demo备注:此处添加的碰撞效果,必须要在场景中添加设置box collider,   rigibody组件

被子弹击中:

void OnTriggerEnter(Collider other)
{
if (other.tag.CompareTo("PlayerRocket") == 0) {
Rocket rocket = other.GetComponent<Rocket> ();
if(rocket != null){
m_life = m_life - rocket.m_power;
if (m_life <= 0) {
//Destroy (this.gameObject);
Instantiate(m_explosionFX,this.transform.position,Quaternion.identity);
Destroy (this.gameObject);
GameManager.Instance.AddScore(m_point);
}
}
}
else if (other.tag.CompareTo ("Player") == 0) {
m_life = 0;
Destroy(this.gameObject);
GameManager.Instance.AddScore(m_point);
}
if (other.tag.CompareTo ("bound") == 0) {
Destroy(this.gameObject);
}
}

整体敌人demo:

using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
public float m_speed = 1;
public float m_rotspeed = 30;
public float m_timer =1.5f;
public float m_life = 10;
public float m_power = 2;
//
public AudioClip m_shotClip;
public AudioSource m_audio;
public Transform m_explosionFX;
public int m_point = 10;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
UpdateMove ();
//OnTriggerEnter ();
}
protected virtual void UpdateMove(){
m_timer = m_timer - Time.deltaTime;
if (m_timer <= 0) {
m_timer = 1.5f;
m_rotspeed = - m_rotspeed;
}
this.transform.Rotate (Vector3.up,m_rotspeed * Time.deltaTime,
Space.World);
this.transform.Translate (new Vector3(0,0,-m_speed * Time.deltaTime));
}
void OnTriggerEnter(Collider other)
{
if (other.tag.CompareTo("PlayerRocket") == 0) {
Rocket rocket = other.GetComponent<Rocket> ();
if(rocket != null){
m_life = m_life - rocket.m_power;
if (m_life <= 0) {
//Destroy (this.gameObject);
Instantiate(m_explosionFX,this.transform.position,Quaternion.identity);
Destroy (this.gameObject);
GameManager.Instance.AddScore(m_point);
}
}
}
else if (other.tag.CompareTo ("Player") == 0) {
m_life = 0;
Destroy(this.gameObject);
GameManager.Instance.AddScore(m_point);
}
if (other.tag.CompareTo ("bound") == 0) {
Destroy(this.gameObject);
}
}
}

5、开始物理碰撞的添加

记住:凡是需要有碰撞效果的必须添加

【Box collider】  :  Is trigger  :ok

【Rigiderbody】:   use gravity -no            Is kinematic -yes

6、开始添加各种tags

根据各种Prefab、及游戏对象,添加tags:Enemy,Player,rocket等等

7、创建SuperEnemy 继承自Enemy

现在需要创建EnemyRocket为SuperEnemy服务了

EnemyRocket的demo:

using UnityEngine;
using System.Collections;
public class EnemyRocket : Rocket {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
m_liveTime = m_liveTime - Time.deltaTime;
if(m_liveTime <= 0){
Destroy(this.gameObject);
}
this.transform.Translate (0,0,-m_speed*Time.deltaTime);
}
void OnTriggerEnter(Collider other){
/*
if (other.tag.CompareTo ("Player") == 0) {
Player player = other.GetComponent<Player>();
if(player != null){
player.m_life -= 1;
}
}
*/
}
}


绑定EnemyRocket,记住、一定要关联文件

同时记住:每个EnemyRocket、SuperEnemy都要加上物理碰撞组件,只有加上了才能显示碰撞效果,

现在来创建SuperEnemy的demo:

using UnityEngine;
using System.Collections;
public class SuperEnemy : Enemy {
public float m_power2 = 5;
public float launchTime = 1f;
public Transform e_rocket;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
UpdateMove ();
launchTime -= Time.deltaTime;
if (launchTime <= 0) {
launchTime = 1f;
Instantiate (e_rocket,this.transform.position,this.transform.rotation);
m_audio.PlayOneShot(m_shotClip);
}
}
protected override void UpdateMove()
{
this.transform.Translate (new Vector3(0,0,-m_speed * Time.deltaTime));
}
//-------------------
void OnTriggerEnter(Collider other)
{
if (other.tag.CompareTo("PlayerRocket") == 0) {
Rocket rocket = other.GetComponent<Rocket> ();
if(rocket != null){
m_life = m_life - rocket.m_power;
if (m_life <= 0) {
//Destroy (this.gameObject);
Instantiate(m_explosionFX,this.transform.position,Quaternion.identity);
Destroy (this.gameObject);
}
}
}
else if (other.tag.CompareTo ("Player") == 0) {
m_life = 0;
Destroy(this.gameObject);
}
if (other.tag.CompareTo ("bound") == 0) {
Destroy(this.gameObject);
}
}
//------------------------
}
8、添加边界效果:

在游戏四周添加bound,(我是用cube来生成的),一旦敌人碰上cube,即自动销毁
9、创建敌人生成器:

EnemySpawn:

using UnityEngine;
using System.Collections;
public class EnemySpawn : MonoBehaviour {
public Transform m_enemy;
public float m_timer = 2.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
m_timer = m_timer - Time.deltaTime;
if (m_timer <= 0) {
m_timer =3.0f;
Instantiate(m_enemy,this.transform.position,Quaternion.identity);
}
}
void OnDrawGizmos(){
Gizmos.DrawIcon (transform.position,"item.png",true);
}
}


   



最后

以上就是敏感电话为你收集整理的Unity3D 太空大战——整理的全部内容,希望文章能够帮你解决Unity3D 太空大战——整理所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部