见缝插针小游戏制作详细步骤
详细步骤
1:打开unity,创建一个2D项目,创建一个放代码的文件夹叫Script,创建一个放预制体的文件夹叫prefab。以后的代码都放在Script文件夹里。预制体都放在prefab文件夹里。
2:网上随便找一个可以用的圆和针的图片。导入unity。
3:将图片设置为2D精灵,如下图片蓝色边框位置(Sprite(2D and UI))。
4:接下来写一个简单的代码让圆旋转起来,代码命名为Rotate。挂在圆上。代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Rotate : MonoBehaviour
{public float speed = 100;//小球旋转速度// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){transform.Rotate(new Vector3(0, 0, -speed * Time.deltaTime));}
}
5:创建两个空物体,分别命名为InsPoint和StartPoint,这是针生成的位置和针等待的位置。InsPoint的Position设为(0,-5,0),StartPoint的Position设为(0,-10,0),这个位置可以按照情况适度调整。最后将他们的图标设置成灰色。
6:将针的2D精灵拖到层级面板中,添加2D刚体并且吧Gravity设置为0。接着创建一个针的代码命名为Pin,把代码挂载到针上。代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Pin : MonoBehaviour
{public bool isReah = false;public bool isFly = false;private Transform starPoint;private Transform dunPos;//圆的位置public float speed = 5;//针移动的速度private Vector3 endPoint;//结束位置// Start is called before the first frame updatevoid Start(){starPoint = GameObject.Find("StartPoint").transform;dunPos = GameObject.Find("dun").transform;endPoint = dunPos.position;endPoint.y -= 4f;}// Update is called once per framevoid Update(){if (isFly == false){if (isReah == false){transform.position = Vector3.MoveTowards(transform.position, starPoint.position, speed * Time.deltaTime);if (Vector3.Distance(transform.position, starPoint.position) < 0.05f){isReah = true;}}}else{transform.position = Vector3.MoveTowards(transform.position, endPoint, speed * Time.deltaTime);if (Vector3.Distance(transform.position, endPoint) < 0.05){isFly = false;transform.parent = dunPos;}}}public void StartFly(){isFly = true;isReah = true;}
}
7:下一步是创建针的预设体,把层级面板里的针直接拖到prefab文件夹中。这样针的预设物已经做好了。然后将层级面板里的针删掉。
8:接下来创建一个空物体命名为GameManger。创建一个text,设置合适大小颜色将它放到圆心的位置。然后创建一个代码命名为GameManger,把代码挂载到空物体GameManger身上。具体代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;public class GameManger : MonoBehaviour
{public GameObject PinPrefab;//针的预设物private Transform insPoint;//实例化位置public Pin currentPin;//当前针public bool isover;public GameObject dun;public Text scoreText;//分数显示的文本public int score = 0;//分数public Camera mainCamera;public int speed = 3;//渐变的速度// Start is called before the first frame updatevoid Start(){insPoint = GameObject.Find("InsPoint").transform;InsPin();mainCamera = Camera.main;//获取主相机}// Update is called once per framevoid Update(){if (isover) return;//获取鼠标点击 0-鼠标左键 1-鼠标右键 2-鼠标中键if (Input.GetMouseButtonDown(0)){score++;scoreText.text = score.ToString();currentPin.StartFly();//Debug.Log("鼠标左键点击了一下");InsPin();}}internal static object Find(string v){throw new NotImplementedException();}//实例化针void InsPin(){currentPin = GameObject.Instantiate(PinPrefab, insPoint.position, PinPrefab.transform.rotation).GetComponent<Pin>();}public void GameOver(){if (isover) return;dun.GetComponent<Rotate>().enabled = false;StartCoroutine(GameOverAnimation());//开启协程isover = true;}IEnumerator GameOverAnimation(){while (true){mainCamera.backgroundColor = Color.Lerp(mainCamera.backgroundColor, Color.red, speed * Time.deltaTime);mainCamera.orthographicSize = Mathf.Lerp(mainCamera.orthographicSize, 4, speed * Time.deltaTime);if (Mathf.Abs(mainCamera.orthographicSize - 4) < 0.01){break;}yield return 0;}yield return new WaitForSeconds(2);SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);}
}
9:回到unity中,将针的预制体拖入Pin Prefab中,将你的圆拖入dun中(这里我的代码以及圆的名字叫dun,记得名字一定要同步),将text拖入Score Text中,将摄像机拖入Main Camera中。具体情况如下图:
10:接下来要做最后一步,就是游戏结束。创建一个代码命名为PinHead,将他挂载到针的身上。代码内容如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PinHead : MonoBehaviour
{private void OnTriggerEnter2D(Collider2D collision){if (collision.tag == "jian"){GameObject.Find("GameManger").GetComponent<GameManger>().GameOver();//游戏结束}}
}
11:回到unity中,选中针的预制体点击Add Component,添加Circle collider2d组件并勾选Is Tirgger。点击Edit Collider在Scene视图中将碰撞体缩放到针的末尾。然后点击Apply应用。然后创建一个名为“jian”的tag(标签)。并选上具体情况如下图:
12:最后呢运行游戏就可以了。
有什么问题欢迎大家提问!
见缝插针小游戏制作详细步骤相关推荐
- unity小游戏制作之见缝插针
unity小游戏制作之见缝插针 先看效果图这个游戏是一个入门的小游戏,主要技术就是圆盘的旋转,针跟随圆盘旋转以及分数累加等 游戏先拖入一个圆,更改格式为2D然后建立脚本使其转动 public clas ...
- Java飞机小游戏制作简单实现详细小结
567881@ftJava小游戏开发 Java飞机小游戏制作简单实现小结 本人原来是个技术小白,寒假我跟着高淇老师的Java300集开始自学Java,跟着做了一个飞机大战小游戏,已经实现了飞机的自由移 ...
- 飞机大战小游戏(超详细)
偷学Python之最后的项目二:飞机大战小游戏(超详细) 古之立大事者,不惟有超世之才,亦必有坚忍不拔之志.--苏轼 甜甜先说 这次用Python中的pygame模块来完成一个飞机大战的小游戏:基本思 ...
- 微信小程序开发详细步骤(企业小程序开发流程)
今天珍奶bb给大家简单唠唠微信小程序开发详细步骤(企业小程序开发流程)? 微信小程序制作流程是什么?微信小程序制作模板套用怎么操作?今天珍奶bb给大家简单唠唠微信小程序制作流程是什么? 在唠微信小程序 ...
- 基于pygame的射击小游戏制作(一)让飞船动起来
基于pygame的射击小游戏制作(一)让飞船动起来 一.文件结构 alien_invasion.py 是整个系统的主文件,用来创建游戏中的一系列对象,ai_settings存储设置.screen存储显 ...
- 基于stm32、0.96寸OLED实现的贪吃蛇小游戏(详细源码注释)
简介:本实验基于stm32最小系统.0.96寸OLED(68*128)和摇杆实现一个经典的贪吃蛇小游戏.项目源码地址:点击下载. 硬件设计: 普通摇杆,0.96寸OLED 单色屏幕(SPI协议通讯), ...
- 基于stm32、0.96寸OLED实现的俄罗斯方块小游戏(详细源码注释)
概述:本实验基于stm32最小系统.0.96寸OLED(68*128)和摇杆实现一个经典的俄罗斯方块小游戏.项目源码地址:点击下载. 硬件要求: 普通摇杆,两个电位器和一个开关组成,左右摇动控制一个电 ...
- 微信小游戏制作坦克大战(四)添加敌方坦克,敌方坦克可以随机移动
微信小游戏制作坦克大战(四)添加敌方坦克,敌方坦克可以随机移动 首先导入敌方坦克素材 重命名为敌方坦克1 敌方坦克也移动到屏幕外面,后面使用克隆体来显示. 我们给敌方坦克添加事件 好的,现在敌方坦克已 ...
- 微信小游戏制作坦克大战(六)碰撞检测,主角坦克碰到敌方坦克、炮弹爆炸
微信小游戏制作坦克大战(六)碰撞检测,主角坦克碰到敌方坦克.炮弹爆炸 导入坦克爆炸效果的图片和声音素材 给主角坦克添加事件 给爆炸动画添加事件 当主角坦克碰到敌方坦克或者炮弹时显示爆炸效果 下一篇文章 ...
最新文章
- React.js 小书 Lesson14 - 实战分析:评论功能(一)
- 【坑爹微信】总有一款接口能坑你到吐血 --- 微信开发经验录
- 神经网络中常用的激活函数
- 设计模式-装饰者模式[Decorator]
- Building Roads(POJ-3625)
- 知识技能归档-SSL协议20210325
- batch echo 输出空格(空行 空白行)
- 怎么通过dd命令分析文件系统
- 软件开发文档编写规范
- linux kvm 的虚拟机处于暂停状态怎么开机 和 KVM-Virsh指令
- C语言移动营业厅程序设计,【程序设计论文】C语言教学的移动应用程序设计(共2486字)...
- 现代软件工程讲义 7 设计阶段 典型用户 - 故事 - 任务 - 具体工作
- 详解在VMware Workstation Pro 15 安装 CentOS 7后配置网络
- matlab 径向偏振光,一种产生椭圆形径向偏振光束的涡旋半波片及系统的制作方法...
- 当你写爬虫遇到APP的请求有加密参数时该怎么办?【初级篇-秒杀模式】
- tsc HPET kvm-clock
- 实例分割模型Mask R-CNN详解:从R-CNN,Fast R-CNN,Faster R-CNN再到Mask R-CNN
- VHDL语言入门整理
- SpringBoot 之 数据访问
- 【H3C实验】H3CSE-园区测试题一(题目)