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;我们一起回顾深度学习的历史…

嵌入式Linux:如何监视子进程

目录 1、wait()函数 2、waitpid()函数 3、SIGCHLD信号 在嵌入式Linux系统中&#xff0c;父进程通常需要创建子进程来执行特定任务&#xff0c;例如处理网络请求、执行计算任务等。监视子进程的状态不仅可以确保资源的合理利用&#xff0c;还能防止僵尸进程的产生&#xff0c…

「 机器人 」扑翼飞行器控制策略浅谈

1. 研究背景 • 自然界中的蜂鸟以极高的机动能力著称,能够在短至0.2秒内完成如急转弯、快速加速、倒飞、躲避威胁等极限机动。这种表现对微型飞行器(Flapping Wing Micro Air Vehicles, FWMAVs)具有重要的仿生启示。 • 目前的微型飞行器距离自然生物的飞行能力仍有相当差距…

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

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

c++ map/multimap容器 学习笔记

1 map的基本概念 简介&#xff1a; map中所有的元素都是pair pair中第一个元素是key&#xff08;键&#xff09;&#xff0c;第二个元素是value&#xff08;值&#xff09; 所有元素都会根据元素的键值自动排序。本质&#xff1a; map/multimap 属于关联式容器&#xff0c;底…

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

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

PyTorch 与 Python 版本对应关系

PyTorch 支持多个 Python 版本&#xff0c;但不同版本的 PyTorch 可能对 Python 版本有不同的要求。一般来说&#xff1a; PyTorch 与 Python 版本对应关系 PyTorch 版本支持的 Python 版本2.2.x3.8 - 3.122.1.x3.8 - 3.112.0.x3.8 - 3.101.13.x3.7 - 3.101.12.x3.7 - 3.101.…

JavaScript系列(47)--音频处理系统详解

JavaScript音频处理系统详解 &#x1f3b5; 今天&#xff0c;让我们深入探讨JavaScript的音频处理系统。Web Audio API为我们提供了强大的音频处理和合成能力&#xff0c;让我们能够在浏览器中实现复杂的音频应用。 音频系统基础概念 &#x1f31f; &#x1f4a1; 小知识&…

FortiOS 存在身份验证绕过导致命令执行漏洞(CVE-2024-55591)

免责声明: 本文旨在提供有关特定漏洞的深入信息,帮助用户充分了解潜在的安全风险。发布此信息的目的在于提升网络安全意识和推动技术进步,未经授权访问系统、网络或应用程序,可能会导致法律责任或严重后果。因此,作者不对读者基于本文内容所采取的任何行为承担责任。读者在…

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…

学历赋

崇岳北峙&#xff0c;紫气东临&#xff1b;学海横流&#xff0c;青云漫卷。连九陌而贯八荒&#xff0c;纳寒门而载贵胄。墨池泛舟&#xff0c;曾照匡衡凿壁之光&#xff1b;杏坛飞絮&#xff0c;犹闻仲尼弦歌之音。然观当下&#xff0c;黉宇接天如笋立&#xff0c;青衫叠浪似云…

支持selenium的chrome driver更新到132.0.6834.110

最近chrome释放新版本&#xff1a;132.0.6834.110 如果运行selenium自动化测试出现以下问题&#xff0c;是需要升级chromedriver才可以解决的。 selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only s…

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…

Excel - Binary和Text两种Compare方法

Option Compare statement VBA里可以定义默认使用的compare方法&#xff1a; Set the string comparison method to Binary. Option Compare Binary That is, "AAA" is less than "aaa". Set the string comparison method to Text. Option Compare Tex…