Unity实现见缝插针小游戏
更新时间:2020年6月25日 10:34 点击:2024
本文实例为大家分享了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经典小游戏汇总
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。
上一篇: Unity实现引导页效果
下一篇: unity实现翻页按钮功能
相关文章
- 这篇文章主要介绍了JavaScript 实现生命游戏的示例步骤,帮助大家更好的理解和学习使用JavaScript,感兴趣的朋友可以了解下...2021-04-24
- 这篇文章主要为大家详细介绍了Unity时间戳的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 这篇文章主要给大家分享的是pygame游戏进行声音添加的方法,这文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!...2021-10-23
- 这篇文章主要为大家详细介绍了C#拼图游戏的编写代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 今天小编跟大家分享13个有趣又好玩的Python小游戏示例代码,教你如何通过边打游戏边学编程!感兴趣的小伙伴快跟随小编一起学习起来...2022-02-23
Unity中 ShaderGraph 实现旋涡传送门效果入门级教程(推荐)
通过Twirl 旋转节点对Gradient Noise 梯度噪声节点进行操作,就可得到一个旋转的旋涡效果。具体实现代码跟随小编一起通过本文学习下吧...2021-07-11基于Python-Pycharm实现的猴子摘桃小游戏(源代码)
这篇文章主要介绍了基于Python-Pycharm实现的猴子摘桃小游戏,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-02-20- 这篇文章主要介绍了python基于tkinter制作图形界面的2048游戏的方法,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下...2021-04-06
- 本文主要介绍了4种延时执行的方法,主要包括Update计时器,Invoke,协程,DoTween,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-07-07
- 这篇文章主要为大家详细介绍了Unity shader实现遮罩效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 这篇文章主要介绍了unity 判断鼠标是否在哪个UI上的两种实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-10
- 这篇文章主要给大家介绍了关于利用unity代码C#封装为dll的相关资料,文中通过图文将实现的方法介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
- 这篇文章主要为大家详细介绍了Unity实现换装系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-04-11
- 最近查看了一些关于开源Android游戏引擎的资料,觉得AndEngine功能强大,例子丰富,更新较快。由于初学,找资料化不少时间,现在将自己在网上收集的比较实用的资料网址贴在这里...2016-09-20
- 这篇文章主要为大家详细介绍了JavaScript仿微信打飞机游戏的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2017-05-09
- 这篇文章主要为大家详细介绍了Unity Shader实现径向模糊效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-08-09
- 这篇文章主要为大家详细介绍了JavaScript实现打字游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-19
- 这篇文章主要介绍了python实现逢七拍腿小游戏的思路,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-05-27
- 这篇文章主要介绍了利用python3 的pygame模块实现塔防游戏,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下...2020-05-08
- 这篇文章主要介绍了unity 实现摄像机绕某点旋转一周,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12