详细步骤
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 update
void Start()
{
}
// Update is called once per frame
void 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 update
void Start()
{
starPoint = GameObject.Find("StartPoint").transform;
dunPos = GameObject.Find("dun").transform;
endPoint = dunPos.position;
endPoint.y -= 4f;
}
// Update is called once per frame
void 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 update
void Start()
{
insPoint = GameObject.Find("InsPoint").transform;
InsPin();
mainCamera = Camera.main;//获取主相机
}
// Update is called once per frame
void 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:最后呢运行游戏就可以了。
有什么问题欢迎大家提问!