unity小游戏-插针游戏

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

public class Pin : MonoBehaviour
{
    public float speed = 15;//插针移动到发射点的速度
    public float speed1 = 25;//插针移动到目标位置点的速度
    private bool isFly = false;//是否往目标点发射
    private bool isReach = false;//是否到达就绪点
    private Transform startPoint;//起始点(空物体)
    private Transform circle;

    private Vector3 targetCirclePos;//Pin的目标位置点

    void Start()
    {
        startPoint = GameObject.Find("StartPoint").transform;//获取就绪点的Transform
        circle = GameObject.Find("Circle").transform;//获取circle的Transform
        targetCirclePos = circle.position;//圆形的位置点
        targetCirclePos.y -= 1.48f;//计算得到Pin的位置点
    }

    void Update()
    {
        if (isFly == false)
        {
            if(isReach == false)
            {
                transform.position = Vector3.MoveTowards(transform.position, startPoint.position, speed1 * Time.deltaTime);//往就绪点发射
                //如果插针和就绪点之间的距离小于0.05,表示插针到达位置点
                if (Vector3.Distance(transform.position, startPoint.position) < 0.05f)
                {
                    isReach = true;
                }
            }
        }
        else
        {
            transform.position = Vector3.MoveTowards(transform.position, targetCirclePos, speed * Time.deltaTime);//往目标点移动
            //如果Pin的位置和目标位置点的距离小于0.05
            if (Vector3.Distance(transform.position, targetCirclePos) < 0.05f)
            {
                transform.position = targetCirclePos;
                transform.parent = circle;//把当前插针的父亲设置为circle  这样能够跟随父物体一起运动
                isFly = false;//不再运动
            }
        }
    }

    public void StartFly()
    {
        isFly = true;
        isReach = true;
    }
}
public class GameManager : MonoBehaviour
{
    private Transform startPoint;//就绪点
    private Transform spawnPoint;//开始点
    public GameObject pinPrefabs;
    private Pin currentPin;
    private bool isGameOver = false;

    private int score = 0;//定义分数变量

    public Text scoreText;//在面板直接拖拽赋值

    private Camera mainCamera;//游戏结束的渐变动画
    public float speed = 3;//设置动画渐变的速度

    // Start is called before the first frame update
    void Start()
    {
        startPoint = GameObject.Find("StartPoint").transform;
        spawnPoint = GameObject.Find("SpawnPoint").transform;
        mainCamera = Camera.main;//获取主相机
        SpawnPin();
    }
    // Update is called once per frame
    void Update()
    {
        if (isGameOver) return;
        if (Input.GetMouseButtonDown(0))//如果鼠标按键按下,分数+1
        {
            score++;
            scoreText.text=score.ToString();//.text的类型是String
            currentPin.StartFly();
            SpawnPin();
        }
    }

    void SpawnPin()
    {
        currentPin = GameObject.Instantiate(pinPrefabs, spawnPoint.position, pinPrefabs.transform.rotation).GetComponent<Pin>();//根据pinPrefabs生成对象
    }

    //处理游戏的失败
    public void GameOver()
    {
        if (isGameOver) return;//如果isGameOver=true 直接游戏结束
        GameObject.Find("Circle").GetComponent<RotateSelf>().enabled = false;//脚本失效
        StartCoroutine(GameOverAnimatiion());//开启协程
        isGameOver = true;//游戏继续
    }

    IEnumerator GameOverAnimatiion()
    {
        while (true)
        {
            mainCamera.backgroundColor = Color.Lerp(mainCamera.backgroundColor, Color.red, speed * Time.deltaTime);//插值,speed参数反映了变化的速度
            mainCamera.orthographicSize = Mathf.Lerp(mainCamera.orthographicSize, 4, speed * Time.deltaTime);//lerp插值运算,朝目标颜色、size的变化
            if (Mathf.Abs(mainCamera.orthographicSize - 4) < 0.01)
            {
                break;
            }
            yield return 0;
        }
        yield return new WaitForSeconds(1);//暂停一秒继续
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);//重新加载场景
    }
}
public class RotateSelf : MonoBehaviour
{
    public float speed = 100;

    // Update is called once per frame
    void Update()
    {
        transform.Rotate(new Vector3(0, 0, speed * Time.deltaTime));
    }
}
public class PinCircleTrigger : MonoBehaviour
{
    // Start is called before the first frame update
    private void OnTriggerEnter2D(Collider2D other)
    {//碰撞监测函数,如果主动碰撞的名字为PinHead,触发碰撞监测函数
        if (other.tag == "PinHead")
        {
            GameObject.Find("GameManager").GetComponent<GameManager>().GameOver();//GameManager对象的GameManager脚本的GameOver方法
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值