Unity实现见缝插针小游戏

所属分类: 软件编程 / C#教程 阅读数: 57
收藏 0 赞 0 分享

本文实例为大家分享了Unity实现见缝插针游戏的具体代码,供大家参考,具体内容如下

控制小球旋转

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RotateSelf : MonoBehaviour {
  //每秒旋转90度
  public float speed = 90;

  // Update is called once per frame
  void Update () {
   //绕Z轴顺针旋转
    transform.Rotate(new Vector3(0, 0, -speed * Time.deltaTime));
  }
}

针头碰撞检测

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();
    }
  }
}

控制针的运动位置

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()
  {
     //针的实例化
    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);
    //重新加载场景
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
  }
}

游戏初始状态和运行结果

更多有趣的经典小游戏实现专题,分享给大家:

C++经典小游戏汇总

python经典小游戏汇总

python俄罗斯方块游戏集合

JavaScript经典游戏 玩不停

java经典小游戏汇总

javascript经典小游戏汇总

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

带你复习c# 托管和非托管资源

这篇文章主要介绍了c# 托管和非托管资源的相关资料,文中讲解非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

C# lambda表达式原理定义及实例详解

这篇文章主要介绍了C# lambda表达式原理定义及实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#关于Task.Yeild()函数的讨论

这篇文章主要介绍了C#中关于Task.Yeild()函数的相关资料,文中讲解非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

实例代码讲解c# 线程(上)

这篇文章主要介绍了讲解c# 线程的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

实例代码讲解c# 线程(下)

这篇文章主要介绍了c# 线程的的相关资料,文中示例代码非常细致,对大家的学习有很大帮助,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

c# rsa加密解密详解

这篇文章主要介绍了c# rsa加密解密的的相关资料,文中代码非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

c#中利用Tu Share获取股票交易信息

这篇文章主要介绍了c#中利用Tu Share获取股票交易信息,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C# MVC 使用LayUI实现下拉框二级联动的功能

这篇文章主要介绍了C# MVC 如何使用LayUI实现下拉框二级联动,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

浅谈C# 字段和属性

这篇文章主要介绍了C# 字段和属性的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

浅谈C# 构造方法(函数)

这篇文章主要介绍了C# 构造方法(函数)的的相关资料,文中讲解非常详细,帮助大家更好的学习C#,感兴趣的朋友可以了解下
收藏 0 赞 0 分享
查看更多