赞
踩

在这个小游戏中我用了4个脚本代码其中有 GameManger, Pin, PinHead ,RotateSelf。
GameManger:游戏管理的代码其中有针的预设物.针的实例化.显示分数的文本以及获取鼠标的点击等代码
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class GameManger : MonoBehaviour { public GameObject PinPrefab;//针的预设物 private Transform insPoint;//实例化的位置 public Pin currentPin; public bool isOver; public GameObject Circle; public Text scoreText; public int score = 0; public Camera mainCamera; public int speed=5; // Start is called before the first frame update void Start() { insPoint = GameObject.Find("InsPoint").transform; InsPin(); mainCamera = Camera.main; } // Update is called once per frame void Update() { if (isOver) return; //0----鼠标左键 1----鼠标右键 2----鼠标中键 if(Input.GetMouseButtonDown(0)) { score++; scoreText.text = score.ToString(); currentPin.StartFiy(); InsPin(); } } //实例化针 void InsPin() { currentPin= GameObject.Instantiate(PinPrefab, insPoint.position, PinPrefab.transform.rotation).GetComponent< Pin >(); } public void GameOver() { if (isOver) return; Circle.GetComponent<RotateSelf>().enabled = false; StartCoroutine(GameOverAnimation()); isOver = 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.01) { break; } yield return 0; } yield return new WaitForSeconds(1); SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); } }
Pin:就是关于游戏中针的代码,其中包括针的飞行速度主要的是对于针什么时候飞怎么飞做的判断。
public class Pin : MonoBehaviour { public bool isReah=false; public bool isFiy = false; private Transform startPoint; private Transform circlePos; public float speed = 8; private Vector3 endPoint; // Start is called before the first frame update void Start() { startPoint = GameObject.Find("StartPoint").transform; circlePos = GameObject.Find("Circle").transform; endPoint = circlePos.position; endPoint.y -= 1.7f; } // Update is called once per frame void Update() { if (isFiy == false) { if (isReah == false) { transform.position = Vector3.MoveTowards(transform.position, startPoint.position, speed * Time.deltaTime); if (Vector3.Distance(transform.position, startPoint.position) < 0.05f) { isReah = true; } } } else { transform.position = Vector3.MoveTowards(transform.position, endPoint, speed * Time.deltaTime); if (Vector3.Distance(transform.position, endPoint) < 0.05) { isFiy = false; transform.parent = circlePos; } } } public void StartFiy() { isFiy = true; isReah = true; } }
PinHead :是针头碰撞的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PinHead : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "PinHead")
{
GameObject.Find("GameManger").GetComponent<GameManger>().GameOver();
}
}
}
RotateSelf:因为是2D所以这个脚本是让圆以Z轴旋转的代码`
using System.Collections; using System.Collections.Generic; using UnityEngine; public class RotateSelf: MonoBehaviour { public float speed = 100; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { transform.Rotate(new Vector3(0, 0, -speed * Time.deltaTime)); } }
完成后游戏的效果
*## 注:代码均有老师代领完成
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。