Unity|小游戏复刻|见缝插针2(C#)

server/2025/2/3 22:32:14/
控制针的运动
  1. 新建一个Pin脚本
    ![[Pasted image 20250125203340.png]]

  2. 将Pin脚本拖到针Pin的下面
    ![[Pasted image 20250125203452.png]]

  3. 保存代码

c#">using UnityEngine;public class Pin : MonoBehaviour
{public float speed = 5;private bool isFly = false;private bool isReach = false;private Transform startPosition;// Start is called once before the first execution of Update after the MonoBehaviour is createdvoid Start(){startPosition = GameObject.Find("StartPosition").transform;}// Update is called once per framevoid Update(){if (isFly == false){if (isReach == false){transform.position = Vector3.MoveTowards(transform.position, startPosition.position, speed * Time.deltaTime) ;if (Vector3.Distance(transform.position, startPosition.position) < 0.05f ){isReach = true ;}}}}
}

![[Pasted image 20250125204640.png]]

控制针的插入

Pin.C#

c#">using UnityEngine;public class Pin : MonoBehaviour
{public float speed = 5;private bool isFly = false;private bool isReach = false;private Transform startPosition;private Transform circle;// Start is called once before the first execution of Update after the MonoBehaviour is createdvoid Start(){startPosition = GameObject.Find("StartPosition").transform;circle = GameObject.Find("Circle").transform;}// Update is called once per framevoid Update(){if (isFly == false){if (isReach == false){transform.position = Vector3.MoveTowards(transform.position, startPosition.position, speed * Time.deltaTime) ;if (Vector3.Distance(transform.position, startPosition.position) < 0.05f ){isReach = true ;}}}else{transform.position = Vector3.MoveTowards(transform.position, circle.position, speed * Time.deltaTime);}}public void StartFly(){isFly = true ;isReach = true;}
}

GameManager.C#

c#">using UnityEngine;public class GameManager : MonoBehaviour
{private Transform startPosition;private Transform spawnPosition;private Pin currentPin;public GameObject pinPrefab;// Start is called once before the first execution of Update after the MonoBehaviour is createdvoid Start(){startPosition = GameObject.Find("StartPosition").transform;spawnPosition = GameObject.Find("SpawnPosition").transform;SpawnPin();}// Update is called once per framevoid Update(){if (Input.GetMouseButtonDown(0)){currentPin.StartFly();}}void SpawnPin(){currentPin = GameObject.Instantiate(pinPrefab, spawnPosition.position, pinPrefab.transform.rotation).GetComponent<Pin>();}
}

![[Pasted image 20250125211919.png]]

控制针的位置和连续发射
  1. 计算Circle和Pin此时位置的y值,为2和0.46,差为1.54
    ![[Pasted image 20250125212940.png]]

Pin

c#">using UnityEngine;public class Pin : MonoBehaviour
{public float speed = 5;private bool isFly = false;private bool isReach = false;private Transform startPosition;private Vector3 targetCirclePos;private Transform circle;// Start is called once before the first execution of Update after the MonoBehaviour is createdvoid Start(){startPosition = GameObject.Find("StartPosition").transform;circle = GameObject.Find("Circle").transform;targetCirclePos = circle.position;targetCirclePos.y -= 1.54f;}// Update is called once per framevoid Update(){if (isFly == false){if (isReach == false){transform.position = Vector3.MoveTowards(transform.position, startPosition.position, speed * Time.deltaTime) ;if (Vector3.Distance(transform.position, startPosition.position) < 0.05f ){isReach = true ;}}}else{transform.position = Vector3.MoveTowards(transform.position, targetCirclePos, speed * Time.deltaTime);}}public void StartFly(){isFly = true ;isReach = true;}
}

![[Pasted image 20250125213317.png]]

使针跟随小球运动

Pin

c#">using UnityEngine;public class Pin : MonoBehaviour
{public float speed = 5;private bool isFly = false;private bool isReach = false;private Transform startPosition;private Vector3 targetCirclePos;private Transform circle;// Start is called once before the first execution of Update after the MonoBehaviour is createdvoid Start(){startPosition = GameObject.Find("StartPosition").transform;circle = GameObject.Find("Circle").transform;targetCirclePos = circle.position;targetCirclePos.y -= 1.54f;}// Update is called once per framevoid Update(){if (isFly == false){if (isReach == false){transform.position = Vector3.MoveTowards(transform.position, startPosition.position, speed * Time.deltaTime) ;if (Vector3.Distance(transform.position, startPosition.position) < 0.05f ){isReach = true ;}}}else{transform.position = Vector3.MoveTowards(transform.position, targetCirclePos, speed * Time.deltaTime);if (Vector3.Distance(transform.position,targetCirclePos) < 0.05f){transform.position = targetCirclePos;transform.parent = circle;isFly = false;}}}public void StartFly(){isFly = true ;isReach = true;}
}

![[Pasted image 20250125213644.png]]

继续生成针

GameManager

c#">using UnityEngine;public class GameManager : MonoBehaviour
{private Transform startPosition;private Transform spawnPosition;private Pin currentPin;public GameObject pinPrefab;// Start is called once before the first execution of Update after the MonoBehaviour is createdvoid Start(){startPosition = GameObject.Find("StartPosition").transform;spawnPosition = GameObject.Find("SpawnPosition").transform;SpawnPin();}// Update is called once per framevoid Update(){if (Input.GetMouseButtonDown(0)){currentPin.StartFly();SpawnPin();}}void SpawnPin(){currentPin = GameObject.Instantiate(pinPrefab, spawnPosition.position, pinPrefab.transform.rotation).GetComponent<Pin>();}
}

![[Pasted image 20250125213923.png]]

针的碰撞和游戏结束
  1. 给Pin的针头添加Circle Collider 2D,勾上触发器,再添加rigidbody 2D,重力设为0
    ![[Pasted image 20250125214546.png]]

  2. 添加PinHead脚本,将其挂载到PinHead上,给PinHead添加上自己添加的PinHead标签
    ![[Pasted image 20250125214933.png]]

  3. 进行碰撞检测
    PinHead

c#">using UnityEngine;public class PinHead : MonoBehaviour
{private void OnTriggerEnter2D(Collider2D collision){if (collision.tag == "PinHead"){GameObject.Find("GameManager").GetComponent<GameManager>().GameOver();}}
}

GameManager

c#">using UnityEngine;
using UnityEngine.UIElements;public class GameManager : MonoBehaviour
{private Transform startPosition;private Transform spawnPosition;private Pin currentPin;private bool isGameOver = false;public GameObject pinPrefab;// Start is called once before the first execution of Update after the MonoBehaviour is createdvoid Start(){startPosition = GameObject.Find("StartPosition").transform;spawnPosition = GameObject.Find("SpawnPosition").transform;SpawnPin();}// Update is called once per framevoid Update(){if (isGameOver) return;if (Input.GetMouseButtonDown(0)){currentPin.StartFly();SpawnPin();}}void SpawnPin(){currentPin = GameObject.Instantiate(pinPrefab, spawnPosition.position, pinPrefab.transform.rotation).GetComponent<Pin>();}public void GameOver(){if (isGameOver) return;GameObject.Find("Circle").GetComponent<RotateSelf>().enabled = false;isGameOver = true;}
}

![[Pasted image 20250125223646.png]]

分数增加

GameManager

c#">using UnityEditor.Tilemaps;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.UIElements;
using TMPro;public class GameManager : MonoBehaviour
{private Transform startPosition;private Transform spawnPosition;private Pin currentPin;private bool isGameOver = false;private int score = 0;public TextMeshProUGUI scoreText;public GameObject pinPrefab;// Start is called once before the first execution of Update after the MonoBehaviour is createdvoid Start(){startPosition = GameObject.Find("StartPosition").transform;spawnPosition = GameObject.Find("SpawnPosition").transform;SpawnPin();}// Update is called once per framevoid Update(){if (isGameOver) return;if (Input.GetMouseButtonDown(0)){score++;scoreText.text = score.ToString();currentPin.StartFly();SpawnPin();}}void SpawnPin(){currentPin = GameObject.Instantiate(pinPrefab, spawnPosition.position, pinPrefab.transform.rotation).GetComponent<Pin>();}public void GameOver(){if (isGameOver) return;GameObject.Find("Circle").GetComponent<RotateSelf>().enabled = false;isGameOver = true;}
}

![[Pasted image 20250125225057.png]]

游戏结束动画

当游戏结束后,将主摄像头背景变为红色,并且使摄像头size变小,使得游戏内容放大
通过camera组件进行控制
GameManager

c#">using UnityEditor.Tilemaps;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.UIElements;
using TMPro;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using System.Collections;public class GameManager : MonoBehaviour
{private Transform startPosition;private Transform spawnPosition;private Pin currentPin;private bool isGameOver = false;private int score = 0;private Camera mainCamera;public TextMeshProUGUI scoreText;public GameObject pinPrefab;public float speed = 3;// Start is called once before the first execution of Update after the MonoBehaviour is createdvoid Start(){startPosition = GameObject.Find("StartPosition").transform;spawnPosition = GameObject.Find("SpawnPosition").transform;mainCamera = Camera.main;SpawnPin();}// Update is called once per framevoid Update(){if (isGameOver) return;if (Input.GetMouseButtonDown(0)){score++;scoreText.text = score.ToString();currentPin.StartFly();SpawnPin();}}void SpawnPin(){currentPin = GameObject.Instantiate(pinPrefab, spawnPosition.position, pinPrefab.transform.rotation).GetComponent<Pin>();}public void GameOver(){if (isGameOver) return;GameObject.Find("Circle").GetComponent<RotateSelf>().enabled = false;StartCoroutine(GameOverAnimation());isGameOver = 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.01f){break;}yield return 0;}yield return new WaitForSeconds(0.2f);SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);}
}

![[Pasted image 20250125232116.png]]


http://www.ppmy.cn/server/164728.html

相关文章

c++可变参数详解

目录 引言 库的基本功能 va_start 宏: va_arg 宏 va_end 宏 va_copy 宏 使用 处理可变参数代码 C11可变参数模板 基本概念 sizeof... 运算符 包扩展 引言 在C编程中&#xff0c;处理不确定数量的参数是一个常见的需求。为了支持这种需求&#xff0c;C标准库提供了 &…

Java泛型深度解析(JDK23)

第一章 泛型革命 1.1 类型安全的进化史 前泛型时代的类型转换隐患 代码的血泪史&#xff08;Java 1.4版示例&#xff09;&#xff1a; List rawList new ArrayList(); rawList.add("Java"); rawList.add(Integer.valueOf(42)); // 编译通过// 灾难在运行时爆发…

FastExcel使用详解

文章目录 FastExcel使用详解一、引言二、环境准备与依赖引入1、Maven 依赖引入2、实体类定义 三、核心操作&#xff1a;读写 Excel1、读取 Excel1.1 自定义监听器1.2 读取文件 2、写入 Excel2.1 简单写入2.2 模板写入 四、Spring Boot 集成示例1、文件上传&#xff08;导入&…

深入浅出并查集(不相交集合实现思路)

引言 并查集&#xff08;Disjoint Set Union&#xff0c;简称DSU&#xff09;是一种用于处理一些不交集的合并及查询问题。它主要支持两种操作&#xff1a;查找&#xff08;Find&#xff09;和合并&#xff08;Union&#xff09;。 查找&#xff1a;确定某个元素属于哪一个子…

XML DOM 解析器

大多数浏览器都内建了供读取和操作 XML 的 XML 解析器。 解析器把 XML 转换为 JavaScript 可存取的对象&#xff08;XML DOM&#xff09;。 XML 解析器 XML DOM 包含了遍历 XML 树&#xff0c;访问、插入及删除节点的方法&#xff08;函数&#xff09;。 然而&#xff0c;在…

LeetCode题练习与总结:在系统中查找重复文件--609

一、题目描述 给你一个目录信息列表 paths &#xff0c;包括目录路径&#xff0c;以及该目录中的所有文件及其内容&#xff0c;请你按路径返回文件系统中的所有重复文件。答案可按 任意顺序 返回。 一组重复的文件至少包括 两个 具有完全相同内容的文件。 输入 列表中的单个…

20250128 大语言模型(Large Language Model, LLM)已成为自然语言处理(NLP)领域的重要突破

大语言模型分析报告 一、引言 随着人工智能技术的不断进步&#xff0c;大语言模型&#xff08;Large Language Model, LLM&#xff09;已成为自然语言处理&#xff08;NLP&#xff09;领域的重要突破。这些模型通过大规模语料数据的预训练&#xff0c;具备理解和生成人类语言…

JS面相对象小案例:自定义安全数组

在JS中&#xff0c;数组不像其他语言&#xff08;java、python&#xff09;中那样安全&#xff0c;它具有动态性和弱类型性&#xff0c;切越界访问没有具体的报错&#xff0c;而是返回空&#xff0c;为提升数组的安全性&#xff0c;我们可以自行定义一个安全数组。 一、增加报…