首先做一个针以及一个目标,可以自选比如箭矢射靶,网上自己找素材都行,注意目标最好是圆的。
图片拖入到unity中需要修改成下面的属性

创建一个目标命名为Circle、针命名为Pin、计算得分的Text、以及三个空物体命名如下:

InsPoint是针生成的地方,StartPoint是游戏开始的地方,两者之间距离远一些,InsPoint、StartPoint、Circle的x轴是一样的

1.修改text的属性
把宽和高修改为3,把字体大小修改为1,调整合适的大小以及调整到合适的位置。


2.在生成好的目标上挂上代码
public class RotateSelf : MonoBehaviour
{
public float speed = 30;//小球旋转的速度
// 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));
//这个是顺时针旋转 如果想要逆时针旋转把-speed前的-号删除即可
}
}
这个是代表中间旋转的目标的速度
3.生成一个针,在针的物体上加入Rigidbody 2D把Gravity设置成0、Capsule Collider 2D调整到合适的大小并且修改Pin的Tag属性为PinHead。
然后在针上挂上代码
public class Pin : MonoBehaviour
{
public bool isReah=false;
public bool isFly=false;
private Transform startPoint;
private Transform circlePos;//圆的位置
public float speed = 3;//针移动的速度
private Vector3 endPoint;//结束位置
// Start is called before the first frame update
void Start()
{
startPoint = GameObject.Find("StartPoint").transform;
circlePos=GameObject.Find("Circle").transform;
endPoint = circlePos.position;
endPoint.y -= 2.6f;
}
// Update is called once per frame
void Update()
{
if (isFly == false)
{
if (isReah == false)
{
transform.position = Vector3.MoveTowards(transform.position, startPoint.position, speed * Time.deltaTime);
if (Vector3.Distance(transform.position, startPoint.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 = circlePos;
}
}
}
public void StartFly() {
isFly = true;
isReah = true;
}
}
这个是检测到针碰到另一个针的时候结束游戏
public class PinHead : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "PinHead")
{
GameObject.Find("GameManger").GetComponent<GameManger>().GameOver();
//游戏结束
}
}
}
然后把针移动到Perfab里生成一个预设体,并且把针的实体删除。
4.在GameManger挂上代码
在GameManger脚本中添加两个命名空间
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManger : MonoBehaviour
{
public GameObject PinPrefab;//针的预设体
private Transform insPoint;//实例化的位置
public Pin currentPin;//当前针
public bool isOver;//判断当前是否结束
public GameObject Circle;
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();
}
}
//实例化针
void InsPin() {
currentPin = GameObject.Instantiate(PinPrefab, insPoint.position, PinPrefab.transform.rotation).GetComponent<Pin>();
}
public void GameOver() {
if (isOver) return;
Circle.GetComponent<RotateSelf>().enabled = false;
isOver = true;
StartCoroutine(GameOverAnimation());//开启协程
}
//协程
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(1);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
按照这个把相应的东西挂载上

5.然后添加游戏结束的效果
修改相机的camera属性

这是修改完的样子。
因为这些也是我初次学习弄的,有什么问题请多多包涵~~~
本文介绍了如何使用Unity3D制作一款2D小游戏——见缝插针。步骤包括创建针和目标对象,设置对象属性,编写检测碰撞的代码,调整UI元素,以及实现游戏结束的逻辑。文章还分享了在游戏开发过程中遇到的问题和解决方案。
704

被折叠的 条评论
为什么被折叠?



