1、Unity3D太空大战Unity3D太空大战源码(1)Enemy的源码:using UnityEngine;using System.Collections;AddComponentMenu(MyGame/Enemy)public class Enemy : MonoBehaviour / 速度 public float m_speed = 1; / 生命 public float m_life = 10; / 旋转速度 protected float m_rotSpeed = 30; / 变向间隔时间 protected float m_timer = 1.5f; protected Tra
2、nsform m_transform; public Transform m_explosionFX; public int m_point = 10; / Use this for initialization void Start () m_transform = this.transform; / Update is called once per frame void Update () UpdateMove(); protected virtual void UpdateMove() m_timer -= Time.deltaTime; if (m_timer = 0) m_time
3、r = 3; /改变旋转方向 m_rotSpeed = -m_rotSpeed; / 旋转方向 m_transform.Rotate(Vector3.up, m_rotSpeed * Time.deltaTime, Space.World); / 前进 m_transform.Translate(new Vector3(0, 0, -m_speed * Time.deltaTime); void OnTriggerEnter(Collider other) if (other.tag.CompareTo(PlayerRocket) = 0) Rocket rocket = other.GetC
4、omponent(); if (rocket != null) m_life -= rocket.m_power; if (m_life = 0) GameManager.Instance.AddScore(m_point); Instantiate(m_explosionFX, m_transform.position, Quaternion.identity); Destroy(this.gameObject); else if (other.tag.CompareTo(Player) = 0) m_life = 0; Instantiate(m_explosionFX, m_transf
5、orm.position, Quaternion.identity); Destroy(this.gameObject); if (other.tag.CompareTo(bound) = 0) m_life = 0; Destroy(this.gameObject); (2)EnemyRocket的源码:using UnityEngine;using System.Collections;AddComponentMenu(MyGame/EnemyRocket)public class EnemyRocket : Rocket void OnTriggerEnter(Collider othe
6、r) if (other.tag.CompareTo(Player) != 0) return; Destroy(this.gameObject); (3)EnemySpawn敌人生成器:using UnityEngine;using System.Collections;AddComponentMenu(MyGame/EnemySpawn)public class EnemySpawn : MonoBehaviour / 敌人的Prefab public Transform m_enemy; / 生成敌人的时间间隔 protected float m_timer = 5; protected
7、 Transform m_transform; / Use this for initialization void Start () m_transform = this.transform; / Update is called once per frame void Update () m_timer -= Time.deltaTime; if (m_timer = 0) m_timer = Random.value * 15.0f; if (m_timer 5) m_timer = 5; Instantiate(m_enemy, m_transform.position, Quater
8、nion.identity); void OnDrawGizmos () Gizmos.DrawIcon (transform.position, item.png, true); (4)GameMannager图形界面:using UnityEngine;using System.Collections;AddComponentMenu(MyGame/GameManager)public class GameManager : MonoBehaviour public static GameManager Instance; /得分 public int m_score = 0; /纪录 p
9、ublic static int m_hiscore = 0; /主角 protected Player m_player; / 背景音乐 public AudioClip m_musicClip; / 声音源 protected AudioSource m_Audio; void Awake() Instance = this; / Use this for initialization void Start () m_Audio = this.audio; / 获取主角 GameObject obj = GameObject.FindGameObjectWithTag(Player); i
10、f (obj != null) m_player = obj.GetComponent(); / Update is called once per frame void Update () / 循环播放背景音乐 if (!m_Audio.isPlaying) m_Audio.clip = m_musicClip; m_Audio.Play(); / 暂停游戏 if (Time.timeScale 0 & Input.GetKeyDown(KeyCode.Escape) Time.timeScale = 0; void OnGUI() / 游戏暂停 if (Time.timeScale = 0
11、) / 继续游戏按钮 if (GUI.Button(new Rect(Screen.width * 0.5f - 50, Screen.height * 0.4f, 100, 30), 继续游戏) Time.timeScale = 1; / 退出游戏按钮 if (GUI.Button(new Rect(Screen.width * 0.5f - 50, Screen.height * 0.6f, 100, 30), 退出游戏) / 退出游戏 Application.Quit(); int life = 0; if (m_player != null) / 获得主角的生命值 life = (in
12、t)m_player.m_life; else / game over / 放大字体 GUI.skin.label.fontSize = 50; / 显示游戏失败 GUI.skin.label.alignment = TextAnchor.LowerCenter; GUI.Label(new Rect(0, Screen.height * 0.2f, Screen.width, 60), 游戏失败); GUI.skin.label.fontSize = 20; / 显示按钮 if (GUI.Button(new Rect(Screen.width * 0.5f - 50, Screen.hei
13、ght * 0.5f, 100, 30), 再试一次) / 读取当前关卡 Application.LoadLevel(Application.loadedLevelName); GUI.skin.label.fontSize = 15; / 显示主角生命 GUI.Label(new Rect(5, 5, 100, 30), 装甲 + life); / 显示最高分 GUI.skin.label.alignment = TextAnchor.LowerCenter; GUI.Label(new Rect(0, 5, Screen.width, 30), 纪录 + m_hiscore); / 显示当
14、前得分 GUI.Label(new Rect(0, 25, Screen.width, 30), 得分 + m_score); / 增加分数 public void AddScore( int point ) m_score += point; / 更新高分纪录 if (m_hiscore m_score) m_hiscore = m_score; (5)Player主飞船的源码:using UnityEngine;using System.Collections;AddComponentMenu(MyGame/Player)public class Player : MonoBehaviou
15、r / 速度 public float m_speed = 1; / 生命 public float m_life = 3; / 子弹prefab public Transform m_rocket; protected Transform m_transform; / 发射子弹频率 float m_rocketRate = 0; / 声音 public AudioClip m_shootClip; / 声音源 protected AudioSource m_audio; / 爆炸特效 public Transform m_explosionFX; / Use this for initial
16、ization void Start () m_transform = this.transform; m_audio = this.audio; / Update is called once per frame void Update () / 纵向移动距离 float movev=0; / 水平移动距离 float moveh=0; / 按上键 if ( Input.GetKey( KeyCode.UpArrow ) ) movev -= m_speed * Time.deltaTime; / 按下键 if ( Input.GetKey( KeyCode.DownArrow ) ) mo
17、vev += m_speed * Time.deltaTime; / 按左键 if ( Input.GetKey( KeyCode.LeftArrow ) ) moveh += m_speed * Time.deltaTime; / 按右键 if ( Input.GetKey( KeyCode.RightArrow ) ) moveh -= m_speed * Time.deltaTime; / 移动 this.m_transform.Translate( new Vector3( moveh, 0, movev ) ); m_rocketRate -= Time.deltaTime; if
18、( m_rocketRate = 0 ) m_rocketRate = 0.1f; if ( Input.GetKey( KeyCode.Space ) | Input.GetMouseButton(0) ) Instantiate( m_rocket, m_transform.position, m_transform.rotation ); / 播放射击声音 m_audio.PlayOneShot(m_shootClip); void OnTriggerEnter(Collider other) if (other.tag.CompareTo(PlayerRocket) != 0) m_l
19、ife -= 1; if (m_life = 0) / 爆炸特效 Instantiate(m_explosionFX, m_transform.position, Quaternion.identity); Destroy(this.gameObject); (6)Rocket子弹的源码:using UnityEngine;using System.Collections;AddComponentMenu(MyGame/Rocket)public class Rocket : MonoBehaviour / 子弹飞行速度 public float m_speed = 10; / 生存时间 pu
20、blic float m_liveTime = 1; / 威力 public float m_power = 1.0f; protected Transform m_trasform; / Use this for initialization void Start () m_trasform = this.transform; / Update is called once per frame void Update () m_liveTime -= Time.deltaTime; if (m_liveTime = 0) Destroy(this.gameObject); m_trasfor
21、m.Translate( new Vector3( 0, 0, -m_speed * Time.deltaTime ) ); void OnTriggerEnter(Collider other) if (other.tag.CompareTo(Enemy)!=0) return; Destroy(this.gameObject); (7)SuperEnemy源码:using UnityEngine;using System.Collections;AddComponentMenu(MyGame/SuperEnemy)public class SuperEnemy : Enemy public
22、 Transform m_rocket; protected float m_fireTimer = 2; protected Transform m_player; void Awake() GameObject obj=GameObject.FindGameObjectWithTag(Player); if ( obj!=null ) m_player=obj.transform; protected override void UpdateMove() m_fireTimer -= Time.deltaTime; if (m_fireTimer = 0) m_fireTimer = 2;
23、 if ( m_player!=null ) Vector3 relativePos = m_transform.position-m_player.position; Instantiate( m_rocket, m_transform.position, Quaternion.LookRotation(relativePos) ); / 前进 m_transform.Translate(new Vector3(0, 0, -m_speed * Time.deltaTime); (8)TitleScreen的源码:using UnityEngine;using System.Collecti
24、ons;AddComponentMenu(MyGame/TitleScreen)public class TitleScreen : MonoBehaviour void OnGUI() / 文字大小 GUI.skin.label.fontSize = 48; / UI中心对齐 GUI.skin.label.alignment = TextAnchor.LowerCenter; / 显示标题 GUI.Label(new Rect(0, 30, Screen.width, 100), 太空大战); / 开始游戏按钮 if (GUI.Button(new Rect(Screen.width * 0.5f - 100, Screen.height * 0.7f, 200, 30), 开始游戏) / 开始读取下一关 Application.LoadLevel(level1);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1