1.unity包不能用中文名
2.camera 旋转x=90,使其拍摄地面
3.x,z都设为0,y设为camera在player上,调好视角
4.player添加mesh collider网格碰撞体,勾convex中凸的,把player collider的网格赋给mesh collider,勾trigger触发器,加刚体,取消use gravity
5.将engine player拖入player,实现飞机尾部特效。将player设为预制体
6.设置主light和补light,两者方向不能同,灯光要向下照才有用
可设置no shadow
7.调整player的材质球
8.加一个quad四边形(调整大小,比例与图片相同)存放背景+【重复移动脚本】Time.time 【此帧开始的时间(只读)。这是以秒计算到游戏开始的时间。也就是说,从游戏开始到到现在所用的时间。】
9.移动脚本变量要是public才能在检视面板修改!
[System.Serializable]使得变量可以被Inspector界面获得
10.子弹也是tif文件,需要加到quad上才能用,shader勾选paticles additive
子弹加上刚体,capsole触发器
子弹发射功能脚本:实现子弹发射位置,时间间隔
11.实现边框,接触边框销毁子弹:添加cube,扩展到边框大小,然后加box触发器,取消mesh render
12.陨石:capsule
其父体无重力刚体mover脚本使其向下forward移动承载脚本
转动脚本:
public float tumble;//倍数
void Start ()
{/*angularVelocity刚体角速度向量
Random.insideUnitSphere返回单位球内一随机点
在Start中赋初始角速度就能使其转动*/
GetComponent< Rigidbody>().angularVelocity = Random .insideUnitSphere * tumble;
}
13.加星空特效
14.制作爆炸脚本,加到三个陨石上
15.创建一个empty叫gameController
定义gameobject飞行物数组,检视可以设置size数组大小,把陨石和敌机都加上去
随机生成enemy脚本,使用协程等待
16.重新游戏脚本直接重载scene即可,界面还会继续
void Update ()
{
if (restart)
{
if (Input .GetKeyDown (KeyCode.R))
{
Application.LoadLevel (Application .loadedLevel);
}
}
}
17.使用标签代替使用name,陨石和敌机都是enemy,这样他们之间碰撞不会爆炸
18.gameController中加音乐play on awake,loop
19.敌机sphere球型碰撞体,放入一个empty,然后加入敌机子弹,自动开火脚本
mover脚本,接触爆炸脚本,机动脚本
敌机子弹+mover,接触爆炸
20.Mathf.Sign(float)参数为负返回-1,否则返回1 即敌机在x正轴则往左飞
Mathf.MoveTowards() 值从第一个参数向第二个移动
float newManeuver = Mathf .MoveTowards (GetComponent().velocity.x, targetManeuver, smoothing * Time.deltaTime);
21.协程等待的时间其他函数会继续执行!
22.UI 计分特效+时间销毁脚本
重复移动背景:
public class Done_BGScroller : MonoBehaviour
{
public float scrollSpeed=-0.25f;
public float tileSizeZ=30;//为quad的scale大小
private Vector3 startPosition;
void Start ()
{
startPosition = transform.position;
}
void Update ()
{ //重复值
float newPosition = Mathf .Repeat(Time.time * scrollSpeed, tileSizeZ);
//位置变化
transform.position = startPosition + Vector3.forward * newPosition;
}
}
子弹发射功能脚本
public GameObject shot;
public Transform shotSpawn;//为player的子物体empty child,用于设置子弹的位置
public float fireRate;
private float nextFire;
void Update ()
{
if (Input .GetButton("Fire1") && Time.time > nextFire)
{ //下一发必须超过firerate发射频率的时间
nextFire = Time.time + fireRate;
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
GetComponent< AudioSource>().Play ();//声音
}
}
爆炸:
public class Done_DestroyByContact : MonoBehaviour
{
public GameObject explosion;//特效
public GameObject playerExplosion;
public int scoreValue=10;
private Done_GameController gameController;
void Start ()
{ //找到gameController脚本
GameObject gameControllerObject = GameObject .FindGameObjectWithTag ("GameController" );
if (gameControllerObject != null )
{
gameController = gameControllerObject.GetComponent ();
}
if (gameController == null )
{
Debug.Log ("Cannot find 'GameController' script" );
}
}
void OnTriggerEnter (Collider other)
{ //如果标签是boundary或enemy,则不触发
if (other.tag == "Boundary" || other.tag == "Enemy")
{
return;
}
//检测
if (explosion != null )
{ //在碰撞处释放爆炸效果
Instantiate(explosion, transform.position, transform.rotation);
}
//玩家爆炸,调用gameover
if (other.tag == "Player" )
{
Instantiate(playerExplosion,
other.transform.position, other.transform.rotation);
gameController.GameOver();
}
//加分
gameController.AddScore(scoreValue);
Destroy (other.gameObject); //销毁撞到陨石的
Destroy (gameObject); //销毁陨石
}
}
飞机控制:
void FixedUpdate ()
{
float moveHorizontal = Input .GetAxis ("Horizontal");
float moveVertical = Input .GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
GetComponent< Rigidbody>().velocity = movement * speed;
GetComponent< Rigidbody>().position = new Vector3
(
Mathf.Clamp
(GetComponent().position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp
(GetComponent().position.z, boundary.zMin, boundary.zMax)
);
//机身倾斜效果
GetComponent< Rigidbody>().rotation = Quaternion .Euler (0.0f, 0.0f, GetComponent().velocity.x * -tilt);
}
}
随机生成,延时,游戏重开:
public class Done_GameController : MonoBehaviour
{
public GameObject [] hazards;//飞行物数组
public Vector3 spawnValues;//随机位置,自行设定范围
public int hazardCount;//一波的数量
public float spawnWait;//生产等待
public float startWait;
public float waveWait;//波之间的等待
//GUI文字
public GUIText scoreText;
public GUIText restartText;
public GUIText gameOverText;
private bool gameOver;
private bool restart;
private int score;
void Start ()
{ //初始化
gameOver = false;
restart = false;
restartText.text = "";
gameOverText.text = "";
score = 0;
UpdateScore ();
StartCoroutine (SpawnWaves ()); //调用
}
void Update ()
{
if (restart)
{
if (Input .GetKeyDown (KeyCode.R))
{
Application.LoadLevel (Application .loadedLevel);
}
}
}
IEnumerator SpawnWaves ()//生成一波,需要协程
{
yield return new WaitForSeconds (startWait); //初始等待
while (true )
{ //生成一波的数量
for (int i = 0; i < hazardCount; i++)
{ //随机生成一个飞行物
GameObject hazard = hazards [Random .Range (0, hazards.Length)];
Vector3 spawnPosition = new Vector3 ( Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion .identity;//四元数
Instantiate (hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds (spawnWait); //等待生成时间
}
yield return new WaitForSeconds (waveWait); //等待下一波的时间
if (gameOver)
{
restartText.text = "Press 'R' for Restart" ;
restart = true;
break;
}
}
}
public void AddScore (int newScoreValue)
{
score += newScoreValue;
UpdateScore ();
}
void UpdateScore ()
{
scoreText.text = "Score: " + score;
}
public void GameOver ()
{
gameOverText.text = "Game Over!";
gameOver = true;
}
}
自动开火:
public class Done_WeaponController : MonoBehaviour
{
public GameObject shot;
public Transform shotSpawn;
public float fireRate;
public float delay;
void Start ()
{
InvokeRepeating ( "Fire", delay, fireRate);
}
void Fire ()
{
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
GetComponent< AudioSource>().Play();
}
}
机动:
public class Done_EvasiveManeuver : MonoBehaviour
{
public Done_Boundary boundary;
public float tilt=10f;//倾斜
public float dodge=5f;//躲闪
public float smoothing;
public Vector2 startWait;//开始机动的等待时间(用vector2是为了用两个范围值233)
public Vector2 maneuverTime;//机动时间
public Vector2 maneuverWait;//机动间隔时间
private float currentSpeed;
private float targetManeuver;//机动的目标值
void Start ()
{
currentSpeed = GetComponent< Rigidbody>().velocity.z;//获得速度
StartCoroutine(Evade());
}
IEnumerator Evade ()
{
//随机等待
yield return new WaitForSeconds ( Random.Range (startWait.x, startWait.y));
while (true )
{ //随机躲闪距离
targetManeuver = Random.Range (1, dodge) * -Mathf .Sign (transform.position.x);
//等待一定时间用于机动
yield return new WaitForSeconds ( Random.Range (maneuverTime.x, maneuverTime.y));
targetManeuver = 0; //机动完毕
yield return new WaitForSeconds ( Random.Range (maneuverWait.x, maneuverWait.y));
}
}
void FixedUpdate ()
{ //取得敌机要移动到的目标机动位置
float newManeuver = Mathf .MoveTowards (GetComponent().velocity.x, targetManeuver, smoothing * Time .deltaTime);
//改变速度方向,目标位置越远向量角度越大
GetComponent< Rigidbody>().velocity = new Vector3 (newManeuver, 0.0f, currentSpeed);
//位置限定,不超边界
GetComponent< Rigidbody>().position = new Vector3
(
Mathf.Clamp(GetComponent().position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp(GetComponent().position.z, boundary.zMin, boundary.zMax)
);
//倾斜度
GetComponent< Rigidbody>().rotation = Quaternion .Euler (0, 0, GetComponent().velocity.x * -tilt);
}
}
本文档为【unity3d 太空射击教程】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。