unity仿微信飞机大战项目

Wesley13
• 阅读 509
开发路线:
    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个单位

 unity仿微信飞机大战项目

B. 建立一个空节点做父节点,在每个背景上添加脚本,BackgroundScroll

 unity仿微信飞机大战项目

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.拖拽第一张精灵图片,层次:渲染的顺序

 unity仿微信飞机大战项目

B.一般2D游戏设置:

 unity仿微信飞机大战项目

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();

    }

    void Update () {

        if (animation)

        {

            timer += Time.deltaTime;

            //每帧所用事件 1f/frameCountPerseconds

            int frameIndex = (int)(timer / (1f / frameCountPerseconds));

            int frame = frameIndex % 2;//取值0,1

            //this.GetComponent().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对象下添加对应位置的空节点放置

unity仿微信飞机大战项目

脚本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);

    }

}

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
前端尾随者 前端尾随者
2年前
sourceTree 添加 ssh key 方法
1.使用git客户的生成公私钥:id\rsa、id\rsa.pub1.1设置Git的username和email:$gitconfigglobaluser.name"xxx"$gitconfig\globaluser.email"xxx.mail@xxx.com"1.2.生成SSH密钥过程:1.2.1.检查是不是已经存在密钥(
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Stella981 Stella981
2年前
Egret学习笔记 (Egret打飞机
运行起来,虽然主角飞机和敌机都在互相发射子弹,但是子弹打中了就和没打中效果是一样的。。这一章我们就来处理子弹和飞机的碰撞问题。我们所有的操作都是基于Main这个容器来做的。所以我就把这个处理放到Main里面,监听Main的_ENTER\_FRAME_事件this.addEventListener(egret.Event.ENTER_FRA
Stella981 Stella981
2年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
Unity 2D游戏开发教程之游戏精灵的开火状态
Unity2D游戏开发教程之游戏精灵的开火状态精灵的开火状态“开火”就是发射子弹的意思,在战争类型的电影或者电视剧中,主角们就爱这么说!本节打算为精灵添加发射子弹的能力。因为本游戏在后面会引入敌人,而精灵最好具备开火的能力,否则会被敌人轻易干掉!具体的实现方法是:(1)导入一个表
Wesley13 Wesley13
2年前
Unity横屏
Android下发现Unity里面的Player设置,并不能完全有效,比如打开了自动旋转,启动的时候还是会横屏,修改XML添加以下代码<applicationandroid:icon"@drawable/ic\_launcher"                    android:label"@string/app\_name"
Wesley13 Wesley13
2年前
Unity5.6.4f1 配置WebGL教程
Unity5.6.4f1发布WebGL的配置教程步骤一:先查看自带的Unity是否yi配置好WebGL的项,若无,则可遵循以下教程来设置!(https://oscimg.oschina.net/oscnet/54612ae3d9b094f1db96b00b1c81a5fe432.png)步骤二:下图是我已经设置好的,未设置
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable