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

控制针的运动
  1. 新建一个Pin脚本
    ![[Pasted image 20250125203340.png]]

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

  3. 保存代码

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#

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#

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

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

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

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

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

GameManager

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

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

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.mzph.cn/diannao/68502.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2025年数学建模美赛 A题分析(3)楼梯使用方向偏好模型

2025年数学建模美赛 A题分析&#xff08;1&#xff09;Testing Time: The Constant Wear On Stairs 2025年数学建模美赛 A题分析&#xff08;2&#xff09;楼梯磨损分析模型 2025年数学建模美赛 A题分析&#xff08;3&#xff09;楼梯使用方向偏好模型 2025年数学建模美赛 A题分…

DeepSeek大模型技术解析:从架构到应用的全面探索

一、引言 在人工智能领域&#xff0c;大模型的发展日新月异&#xff0c;其中DeepSeek大模型凭借其卓越的性能和广泛的应用场景&#xff0c;迅速成为业界的焦点。本文旨在深入剖析DeepSeek大模型的技术细节&#xff0c;从架构到应用进行全面探索&#xff0c;以期为读者提供一个…

「AI学习笔记」深度学习的起源与发展:从神经网络到大数据(二)

深度学习&#xff08;DL&#xff09;是现代人工智能&#xff08;AI&#xff09;的核心之一&#xff0c;但它并不是一夜之间出现的技术。从最初的理论提出到如今的广泛应用&#xff0c;深度学习经历了几乎一个世纪的不断探索与发展。今天&#xff0c;我们一起回顾深度学习的历史…

渗透测试之WAF规则触发绕过规则之规则库绕过方式

目录 Waf触发规则的绕过 特殊字符替换空格 实例 特殊字符拼接绕过waf Mysql 内置得方法 注释包含关键字 实例 Waf触发规则的绕过 特殊字符替换空格 用一些特殊字符代替空格&#xff0c;比如在mysql中%0a是换行&#xff0c;可以代替空格 这个方法也可以部分绕过最新版本的…

深入理解若依RuoYi-Vue数据字典设计与实现

深入理解若依数据字典设计与实现 一、Vue2版本主要文件目录 组件目录src/components&#xff1a;数据字典组件、字典标签组件 工具目录src/utils&#xff1a;字典工具类 store目录src/store&#xff1a;字典数据 main.js&#xff1a;字典数据初始化 页面使用字典例子&#xf…

Linux网络之TCP

Socket编程--TCP TCP与UDP协议使用的套接字接口比较相似, 但TCP需要使用的接口更多, 细节也会更多. 接口 socket和bind不仅udp需要用到, tcp也需要. 此外还要用到三个函数: 服务端 1. int listen(int sockfd, int backlog); 头文件#include <sys/socket.h> 功能: …

GIS与相关专业软件汇总

闲来无事突然想整理一下看看 GIS及相关领域 究竟有多少软件或者工具包等。 我询问了几个AI工具并汇总了一个软件汇总&#xff0c;不搜不知道&#xff0c;一搜吓一跳&#xff0c;搜索出来了大量的软件&#xff0c;大部分软件或者工具包都没有见过&#xff0c;不知大家还有没有要…

(四)线程 和 进程 及相关知识点

目录 一、线程和进程 &#xff08;1&#xff09;进程 &#xff08;2&#xff09;线程 &#xff08;3&#xff09;区别 二、串行、并发、并行 &#xff08;1&#xff09;串行 &#xff08;2&#xff09;并行 &#xff08;3&#xff09;并发 三、爬虫中的线程和进程 &am…

python爬虫入门(一) - requests库与re库,一个简单的爬虫程序

目录 web请求与requests库 1. web请求 1.1 客户端渲染与服务端渲染 1.2 抓包 1.3 HTTP状态代码 2. requests库 2.1 requests模块的下载 2.2 发送请求头与请求参数 2.3 GET请求与POST请求 GET请求的例子&#xff1a; POST请求的例子&#xff1a; 3. 案例&#xff1a;…

Luzmo 专为SaaS公司设计的嵌入式数据分析平台

Luzmo 是一款嵌入式数据分析平台&#xff0c;专为 SaaS 公司设计&#xff0c;旨在通过直观的可视化和快速开发流程简化数据驱动决策。以下是关于 Luzmo 的详细介绍&#xff1a; 1. 背景与定位 Luzmo 前身为 Cumul.io &#xff0c;专注于为 SaaS 公司提供嵌入式分析解决方案。…

在虚拟机里运行frida-server以实现对虚拟机目标软件的监测和修改参数(一)(android Google Api 35高版本版)

frida-server下载路径 我这里选择较高版本的frida-server-16.6.6-android-x86_64 以root身份启动adb 或 直接在android studio中打开 adb root 如果使用android studio打开的话&#xff0c;最好选择google api的虚拟机&#xff0c;默认以root模式开启 跳转到下载的frida-se…

C#编译报错: error CS1069: 未能在命名空间“System.Windows.Markup”中找到类型名“IComponentConnector”

文章目录 问题现象解决方案 问题现象 一个以前使用.NET Framwork 3.0框架开发的项目&#xff0c;在框架升级到.NET Framwork 4.7.2后&#xff0c; 如下代码&#xff1a; #pragma checksum "..\..\XpsViewer.xaml" "{8829d00f-11b8-4213-878b-770e8597ac16}&qu…

能源新动向:智慧能源平台助力推动新型电力负荷管理系统建设

背景 国家能源局近日发布《关于支持电力领域新型经营主体创新发展的指导意见》&#xff0c;鼓励支持具备条件的工业企业、工业园区等开展智能微电网建设&#xff0c;通过聚合分布式光伏、分散式风电、新型储能、可调节负荷等资源&#xff0c;为电力系统提供灵活调节能力&#x…

用WinForm如何制作简易计算器

首先我们要自己搭好页面 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;namespace _7_简易计算…

论文笔记(六十三)Understanding Diffusion Models: A Unified Perspective(四)

Understanding Diffusion Models: A Unified Perspective&#xff08;四&#xff09; 文章概括学习扩散噪声参数&#xff08;Learning Diffusion Noise Parameters&#xff09;三种等效的解释&#xff08;Three Equivalent Interpretations&#xff09; 文章概括 引用&#xf…

【数据结构】(1)集合类的认识

一、什么是数据结构 1、数据结构的定义 数据结构就是存储、组织数据的方式&#xff0c;即相互之间存在一种或多种关系的数据元素的集合。 2、学习数据结构的目的 在实际开发中&#xff0c;我们需要使用大量的数据。为了高效地管理这些数据&#xff0c;实现增删改查等操作&…

Java 实现Excel转HTML、或HTML转Excel

Excel是一种电子表格格式&#xff0c;广泛用于数据处理和分析&#xff0c;而HTM则是一种用于创建网页的标记语言。虽然两者在用途上存在差异&#xff0c;但有时我们需要将数据从一种格式转换为另一种格式&#xff0c;以便更好地利用和展示数据。本文将介绍如何通过 Java 实现 E…

【C语言】结构体与共用体深入解析

在C语言中&#xff0c;结构体&#xff08;struct&#xff09;和共用体&#xff08;union&#xff09;都是用来存储不同类型数据的复合数据类型&#xff0c;它们在程序设计中具有重要的作用。 推荐阅读&#xff1a;操作符详细解说&#xff0c;让你的编程技能更上一层楼 1. 结构体…

思维练习题

目录 第一章 假设法1.题目1. 如何问问题2. 他们的职业是分别什么3. 谁做对了4. 鞋子的颜色 2.答案 空闲时间写一些思维题来锻炼下思维逻辑&#xff08;题目均收集自网上&#xff0c;分析推理为自己所写&#xff09;。 第一章 假设法 一个真实的假设往往可以让事实呈现眼前&…

【C++高并发服务器WebServer】-10:网络编程基础概述

本文目录 一、MAC地址二、IP地址三、子网掩码四、TCP/IP四层模型五、协议六、socket七、字节序 一、MAC地址 网卡是一块被设计用来允许计算机在计算机网络上进行通讯的计算机硬件&#xff0c;又称为网络适配器或网络接口卡NIC。其拥有 MAC 地址&#xff0c;属于 OSI模型的第2层…