注:本小游戏共两章,第一章讲界面的制作,第二章讲后台代码控制接球。
(工程中所使用到的所有图片都是我自己PS画的,比较粗糙,不太美观,能看就行,哈哈。)
传送门:
帽子接球小游戏(一)--制作UI面板
资源下载:
https://pan.baidu.com/s/1TYifaons2CQe_Iha1GqZ9g
提取码: u3bd
接球的实现:
1、球的生成
(1)新建一个空对象,命名为GameControlor,用来控制实例化球的高度。它的位置在整个面板的最上方离顶部一点距离,注意不能离视图的顶部太高,以免球下落太快来不及接。
(2)创建一个C#脚本,命名为GameControlor,来控制球的实例化,使得球从天上一个一个掉落。
编辑如下代码: 把编辑好的脚本挂在GameControlor上,并设置实例化的球(即boll的预制体)。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class GameControlor : MonoBehaviour { public GameObject ball; //球的预制体 private float maxwidth; //最大宽度 private float time = 2; GameObject newball; //新球体 void Start () { //将屏幕宽度转换成世界坐标 Vector3 screenPos = new Vector3(Screen.width, 0, 0); Vector3 moveWidth = Camera.main.ScreenToWorldPoint(screenPos); //获取球自身的宽度 float ballWidth = ball.GetComponent<Renderer>().bounds.extents.x; //计算球实例化位置的宽度 maxwidth = moveWidth.x - ballWidth; } void Update () { time -= Time.deltaTime; if (time <= 0) { //产生一个随机数,代表下一个实例化球体所需的时间 time = Random.Range(1f, 1.5f); float posX = Random.Range(-maxwidth, maxwidth); Vector3 spawnPosition = new Vector3(posX, transform.position.y, 0); //实例化球体,5秒后销毁 newball = (GameObject)Instantiate(ball, spawnPosition, Quaternion.identity); Destroy(newball, 5); } } }2、创建一个C#脚本,命名为HatControlor,用来控制帽子移动和接球。帽子跟随鼠标的移动在水平方向上移动,在竖直方向上保持不变。球进入帽子之后会被删除,这样帽子可以持续接收球,同时统计接到球的个数。没接住的球落在草地上,一段时间后消失。
编辑如下代码:把该脚本挂在Hat_0上,其中effect为进球时产生的火花粒子,font为显示接球数的字体。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class HatControlor : MonoBehaviour { public GameObject effect;//火花粒子 Vector3 rawPosition; Vector3 hatPosition; //帽子的坐标 float maxWidth; //坐标最大值 public static int sum=0; //进球数 public Font font; //字体 void Start () { //把屏幕宽度转换成世界坐标 Vector3 screenPos = new Vector3(Screen.width, 0, 0); Vector3 moveWidth = Camera.main.ScreenToWorldPoint(screenPos); //帽子的宽度 float hatWidth = GetComponent<Renderer>().bounds.extents.x; //获取帽子初始位置 hatPosition = transform.position; //计算帽子的移动宽度 maxWidth = moveWidth.x - hatWidth; } void Update () { rawPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);//把鼠标的屏幕坐标转化为世界坐标 hatPosition = new Vector3(rawPosition.x, hatPosition.y, 0);//设置帽子的位置 hatPosition.x = Mathf.Clamp(hatPosition.x, -maxWidth, maxWidth);//帽子的移动范围 GetComponent<Rigidbody2D>().MovePosition(hatPosition);//帽子移动 } void OnTriggerEnter2D(Collider2D col)//有碰撞体进入帽子触发器 { sum++;//累计进入帽子的球数 GameObject newEffect = (GameObject)Instantiate(effect,transform.position,effect.transform.rotation);//生成新的火花 newEffect.transform.parent = transform;//火花的父节点为当前物体 Destroy(col.gameObject);//销毁进入帽子的物体 Destroy(newEffect, 1.0f);//1秒后销毁火花 } void OnGUI()//统计接住球的个数 { GUIStyle style = new GUIStyle(); style.font = font; //设置字体 style.fontSize = 25; //字体大小 style.normal.textColor = Color.black; //字体颜色(黑色) //显示的位置及大小 GUILayout.BeginArea(new Rect(5, 5, 500, 100)); GUILayout.Label("接球数:" + sum, style, GUILayout.Width(200), GUILayout.Height(50)); GUILayout.EndArea(); } }3、蝴蝶可以停留在花上面,接下来让大雁从左边飞到右边。
新建C#脚本,命名为SwanMove,挂在大雁上。
编辑代码:
using UnityEngine; public class SwanMove : MonoBehaviour { float moveSpeed = 2;//大雁飞行速度 void Start () { transform.position = new Vector3(-10, 2, 0); //大雁的初始位置 } void Update () { //如果大雁飞行的横坐标大于10,回到初始位置 if (transform.position.x > 10) transform.position = new Vector3(-10, 2, 0); else//大雁从左往右飞行 transform.Translate(Vector3.right * moveSpeed * Time.deltaTime); } }4、最后来看一下整体效果: