Unity實(shí)現(xiàn)見(jiàn)縫插針小游戲
本文實(shí)例為大家分享了Unity實(shí)現(xiàn)見(jiàn)縫插針游戲的具體代碼,供大家參考,具體內(nèi)容如下
控制小球旋轉(zhuǎn)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateSelf : MonoBehaviour {
//每秒旋轉(zhuǎn)90度
public float speed = 90;
// Update is called once per frame
void Update () {
//繞Z軸順針旋轉(zhuǎn)
transform.Rotate(new Vector3(0, 0, -speed * Time.deltaTime));
}
}

針頭碰撞檢測(cè)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PinHead : MonoBehaviour {
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "PinHead")
{
GameObject.Find("GameManager").GetComponent<GameManager>().GameOver();
}
}
}
控制針的運(yùn)動(dòng)位置
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pin : MonoBehaviour {
public float speed = 5;
private bool isFly = false;
private bool isReach = false;
private Transform startPoint;
private Vector3 targetCirclePos;
private Transform circle;
// Use this for initialization
void Start () {
startPoint = GameObject.Find("StartPoint").transform;
circle = GameObject.FindGameObjectWithTag("Circle").transform;
targetCirclePos = circle.position;
targetCirclePos.y -= 1.55f;
}
// Update is called once per frame
void Update () {
if (isFly == false)
{
if (isReach == false)
{
transform.position = Vector3.MoveTowards(transform.position, startPoint.position, speed * Time.deltaTime);
if (Vector3.Distance(transform.position, startPoint.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;
}
}
游戲管理
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour {
private Transform startPoint;
private Transform spawnPoint;
private Pin currentPin;
private bool isGameOver = false;
private int score = 0;
private Camera mainCamera;
public Text scoreText;
public GameObject pinPrefab;
public float speed = 3;
// Use this for initialization
void Start () {
startPoint = GameObject.Find("StartPoint").transform;
spawnPoint = GameObject.Find("SpawnPoint").transform;
mainCamera = Camera.main;
SpawnPin();
}
private void Update()
{
if (isGameOver) return;
if (Input.GetMouseButtonDown(0))
{
score++;
scoreText.text = score.ToString();
currentPin.StartFly();
SpawnPin();
}
}
void SpawnPin()
{
//針的實(shí)例化
currentPin = GameObject.Instantiate(pinPrefab, spawnPoint.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);
//重新加載場(chǎng)景
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
游戲初始狀態(tài)和運(yùn)行結(jié)果


更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#基礎(chǔ):基于const與readonly的深入研究
本篇文章是對(duì)c#中const與readonly進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
在C#的類或結(jié)構(gòu)中重寫(xiě)ToString方法的用法簡(jiǎn)介
這篇文章主要介紹了在C#的類或結(jié)構(gòu)中重寫(xiě)ToString方法的用法簡(jiǎn)介,需要的朋友可以參考下2016-01-01
C#?利用Autofac批量接口注入依賴的問(wèn)題小結(jié)
這篇文章主要介紹了C#?利用Autofac批量接口注入依賴的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
C#從數(shù)據(jù)庫(kù)讀取圖片并保存的兩種方法
這篇文章主要介紹了C#從數(shù)據(jù)庫(kù)讀取圖片并保存的方法,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-01-01
C#使用Stack<T>進(jìn)行堆棧設(shè)計(jì)的實(shí)現(xiàn)
堆棧代表了一個(gè)后進(jìn)先出的對(duì)象集合,當(dāng)您需要對(duì)各項(xiàng)進(jìn)行后進(jìn)先出的訪問(wèn)時(shí),則使用堆棧,本文主要介紹了C#使用Stack<T>類進(jìn)行堆棧設(shè)計(jì)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),感興趣的可以了解一下2024-03-03

