開發路線: 1,游戲背景(連續播放) 2,添加主角 3,設置游戲主角的動畫 4,添加兩種子彈並設置子彈的運動 5,添加三種子彈 設置子彈的自動生成和運動 6,添加兩種獎勵物品 設置獎勵物品的自動生成和運動 7,設置主角的控制 7.1檢測手指觸摸 7.2問題:防止主角飛出屏幕 8,設置Tag 添加子彈和敵人的碰撞 9,設計敵人 0 1 2 震動動畫和爆炸效果 10,添加腳本GameManager做游戲的控制 11,統計分數
1,新建項目,導入資源,平台匹配;
2,背景循環(一般是2/3個背景精靈切換);
A. 精靈Sprite :Pixels Per Unit 是精靈的尺寸在unity之后的換算問題
栗子:圖片高度為852像素,導入后顯示的為8個單位
B. 建立一個空節點做父節點,在每個背景上添加腳本,BackgroundScroll
C.腳本; BackgroundScroll.cs
private static float moveSpeed = 3.0f;
void Start () {
}
void Update () {
//move down
this.transform.Translate(Vector3.down * moveSpeed * Time.deltaTime);
//limit
Vector3 pos = transform.position;
if(pos.y <= -8.52f)
{
this.transform.position = new Vector3(pos.x, pos.y + 8.52f * 2, pos.z);
}
}
3.制作主角動畫:(序列幀實現動畫)
A.拖拽第一張精靈圖片,層次:渲染的順序
B.一般2D游戲設置:
C. 分別設置不同的層級:一般0-;數字越小先渲染,然后會被后渲染的遮擋住;
D. 設置主角為character,后添加腳本 Hero;
腳本:using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hero : MonoBehaviour {
//標志位,控制動畫播放
public bool animation = true;
//每秒播放幀數 五遍動畫 一個動畫播放2幀
public int frameCountPerseconds = 10;
//計時器
public float timer = 0;
//sprites
public Sprite[] sprites;
//2
private SpriteRenderer spriteRenderer;
void Start () {
spriteRenderer = this.GetComponent<SpriteRenderer>();
}
void Update () {
if (animation)
{
timer += Time.deltaTime;
//每幀所用事件 1f/frameCountPerseconds
int frameIndex = (int)(timer / (1f / frameCountPerseconds));
int frame = frameIndex % 2;//取值0,1
//this.GetComponent<SpriteRenderer>().sprite = sprites[frame];//消耗性能
spriteRenderer.sprite = sprites[frame];
}
}
}
E.子彈;character層;添加腳本Bullet:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour {
public float moveSpeed = 2;
void Start () { }
void Update () {
transform.Translate(Vector3.up * Time.deltaTime * moveSpeed);
//limit
if(transform.position.y >=4.3f)
{
Destroy(this.gameObject);
}
}
}
F.在Hero對象下添加對應位置的空節點放置
腳本Gun: 放置不同的子彈預制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gun : MonoBehaviour {
public float rate = 0.2f;
public GameObject bullet;
public void Fire()
{
GameObject.Instantiate(bullet, transform.position, Quaternion.identity);//第三個參數:旋轉角度,四元素的一個(無旋轉的)靜態變量
}
public void OpenFire()
{
InvokeRepeating("Fire", 1, rate);
}
void Start () {
OpenFire();
}
}
G.敵人,三種類型添加腳本Enemy,之后掛載 修改對應的hp和move_speed
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Enemy : MonoBehaviour {
public int hp = 1;
public float speed = 2;
public int score = 100;
void Update () {
this.transform.Translate(Vector3.down * speed * Time.deltaTime);
if(this.transform.position.y <= -5.6f)
{
Destroy(this.gameObject);
}
}
}
H.獎勵:兩種 腳本Award:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Award : MonoBehaviour {
public int type = 0;//0 gun 1 explose
public float speed = 1.5f;
void Update () {
this.transform.Translate(Vector3.down * speed * Time.deltaTime);
if(this.transform.position.y <= -4.5f)
{
Destroy(this.gameObject);
}
}
}
- 上述諸多對象做成預制,添加空節點,命名Spawn(用於生成對象)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawn : MonoBehaviour {
public GameObject enemy0Prefab;
public float enemy0Rate = 0.5f;
void Start () {
InvokeRepeating("createEnemy0", 1, enemy0Rate);
}
public void createEnemy0()
{
//隨機pos.x
float pos_x = Random.Range(-2.15f, 2.15f);
GameObject.Instantiate(enemy0Prefab,new Vector3(pos_x,transform.position.y,transform.position.z),Quaternion.identity);
}
}