欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 资讯 > Unity|小游戏复刻|见缝插针2(C#)

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

2025/1/31 1:49:49 来源:https://blog.csdn.net/CoderZzz6310/article/details/145360538  浏览:    关键词: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]]

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com