见缝插针

34 篇文章 0 订阅


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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我在玩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值