BallRotation
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallRotation : MonoBehaviour
{
public float speed = 90;
void Update()
{
transform.Rotate(new Vector3(0, 0, speed*Time .deltaTime));
}
}
GameManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
private Transform spot;//标签1
private Transform spot2;//标签2
public GameObject pinPfaber;//预制体
private pin pin_;//针代码
private bool isGameOver = false;//判读游戏是否结束
public int 分数;
public Text Text;
private Camera maiCamera;//定义摄像机
public float speed = 5;
void Start()
{
spot = GameObject.Find("spot").transform;//获取标签1位置
spot2 = GameObject.Find("spot2").transform;
InstantiatePin();//开局调用生成一次针
maiCamera = Camera.main;//等于主摄像机
}
private void FixedUpdate()
{
if (isGameOver) return;//如果结束就直接返回,不继续调用
if (Input.GetMouseButtonDown(0))
{
pin_.StartFly();//调用到另一个代码的方法
InstantiatePin();//调用生成一次针
分数++;//分数自身+1
Text.text = 分数.ToString();//转为字符串类型
}
}
void InstantiatePin()
{
pin_ = GameObject.Instantiate(pinPfaber, spot2.position, pinPfaber.transform.rotation).GetComponent<pin>();//实例化生成到,地图外的点,预制体的旋转
}
public void GamOver()//游戏结束
{
if (isGameOver) return;
GameObject.Find("Circle").GetComponent<BallRotation>().enabled = false;//找到大圆的组件代码让他停止
isGameOver = true;
StartCoroutine(GameOverAnimation());
}
IEnumerator GameOverAnimation()//协程
{
while (true)
{
maiCamera.backgroundColor = Color.Lerp(maiCamera.backgroundColor, Color.blue, speed * Time.deltaTime);//渐变颜色
maiCamera.orthographicSize = Mathf.Lerp(maiCamera.orthographicSize, 4, speed * Time.deltaTime);//摄像机推进放大
if (Mathf.Abs(maiCamera.orthographicSize - 4) < 0.01f)//达到目标值就跳出循环
{
break;
}
yield return 0;//死循环,循环一次暂停一帧
}
yield return new WaitForSeconds(1);//暂停一秒
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);//重新开始
}
}
Pin
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 spot;//标签1
private Transform circle;//大圆
public Vector3 target;//针的目标位置
void Start()
{
spot = GameObject.Find("spot").transform;//获取标签1位置
circle = GameObject.Find("Circle").transform;//获取大圆位置
target = circle.position;//目标点等于大圆的位置
target.y -= 2.1f;//减去这个数值(这个数值是针位置减去大圆位置得出来的)他俩就能空开针
}
void Update()
{
if(isFly==false)
{
if (isReach==false)
{
//向标签1位置移动过去
transform.position = Vector3.MoveTowards(transform.position, spot.position, speed * Time.deltaTime);
if (Vector3.Distance(transform.position,spot.position)<0.05f)//优化
{
isReach = true;
}
}
}
else
{
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);//发射到大圆
if (Vector3.Distance(transform.position, target)<0.05f)//如果目标位置小于0.05就执行下面
{
transform.position = target;//位置等于针的目标位置
transform.parent = circle;//父物体等于大圆
isFly = false;
}
}
}
public void StartFly()
{
isFly = true;
isReach = true;
}
}
**PinHeade **
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PinHeade : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag==“PinHeade”)//碰到针头标签调用
{
GameObject.Find(“GameManager”).GetComponent< GameManager >().GamOver();
}
}
}