unity仿微信飞机大战项目

开发路线:
	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);

        }

    }

}

  1. 上述诸多对象做成预制,添加空节点,命名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);

    }

}

 

原文地址:https://www.cnblogs.com/allyh/p/10327174.html