其他分享
首页 > 其他分享> > Fire Balls 07——砖块的淡出,消失以及砖塔的下落

Fire Balls 07——砖块的淡出,消失以及砖塔的下落

作者:互联网

版权申明:

  1. 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123
  2. 优梦创客的官方博客:https://91make.top
  3. 优梦创客的游戏讲堂:https://91make.ke.qq.com
  4. 『优梦创客』的微信公众号:umaketop

目标

砖块的消失

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Tower : MonoBehaviour
{
    public int Height;
    public Color[] Colors = new Color[2];
    public GameObject brickPrefab;
    public List<GameObject> bricks = new List<GameObject>();
    public bool isRisenUp = false;//砖块是不是全部从地下钻上来了
    // Start is called before the first frame update
    void Start()
    {
        for(int i = 0; i < Height; i++)
        {
            GameObject goBrick = Instantiate(brickPrefab);
            goBrick.transform.SetParent(transform,false );
            goBrick.transform.Translate(0, i, 0,Space.Self);
            goBrick.GetComponent<MeshRenderer>().material.color = Colors[i % 2];
            goBrick.transform.Rotate(0, i*10, 0,Space.Self);
            bricks.Add(goBrick);
        }
        StartCoroutine(RiseUpFormUnderground());
    }
    IEnumerator RiseUpFormUnderground()
    {
        transform.Translate(0, -Height * transform.lossyScale.y, 0, Space.Self);
        while (transform.position.y<0.5*transform.lossyScale.y)
        {
            transform.Translate(0, 5 * Time.deltaTime, 0, Space.Self);
            yield return null;
        }
        isRisenUp = true;
    }

    // Update is called once per frame
    void Update()
    {
        transform.Rotate(0, 45 * Time.deltaTime, 0,Space.Self);
    }
    public void DropTower()
    {
       
        //启动一个协成
        StartCoroutine(DestoryAndDrop());  
    }

    IEnumerator DestoryAndDrop()
    {
        //第一步:找到最底下一层砖块,然后销毁
        GameObject brick = bricks[0];
        bricks.RemoveAt(0);
        //第二步:设置砖块进入淡出动画状态
        
        //第三步:让上面的每一层砖块往下落一层
        
        //第四步,销毁砖块
        Destroy(brick,2f);
        yield return null;
    }
}
 private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.tag == "Tower")
        {
            Destroy(gameObject);
            collision.transform.parent.GetComponent<Tower>().DropTower();
        }
        if(collision.gameObject.tag == "Arch")
        {
            lober.endPos = player.transform.position;

            lober.enabled = true;

            player.gameover = true;
        }
    }
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Brick : MonoBehaviour
{
    private bool isFadeout = false;
    public Vector3 targetScale;//目标缩放大小
    public float speed = 0.5f;//淡出的速度
    private void Start()
    {
        targetScale = transform.localScale * 3;
    }
    internal void setFadeout()
    {
        isFadeout = true;
    }
    private void Update()
    {
        if (isFadeout)//如果砖块正在淡出
        {
            //1.砖块变大
            float t = 1 / speed * Time.deltaTime;
            transform.localScale = Vector3.Lerp(transform.localScale, targetScale, t);
            //2.砖块颜色变透明
            Color c = GetComponent<MeshRenderer>().material.color;
            c.a = Mathf.Lerp(c.a, 0, t);
            GetComponent<MeshRenderer>().material.color = c;
        }
    }
}
 IEnumerator DestoryAndDrop()
    {
        //第一步:找到最底下一层砖块,然后销毁
        GameObject brick = bricks[0];
        bricks.RemoveAt(0);
        //第二步:设置砖块进入淡出动画状态
        brick.GetComponent<Brick>().setFadeout();
        //第三步:让上面的每一层砖块往下落一层
        
        //第四步,销毁砖块
        Destroy(brick,2f);
        yield return null;
    }

砖块的下落

    IEnumerator DestoryAndDrop()
    {
        //第一步:找到最底下一层砖块,然后销毁
        GameObject brick = bricks[0];
        bricks.RemoveAt(0);
        //第二步:设置砖块进入淡出动画状态
        brick.GetComponent<Brick>().setFadeout();
        //第三步:让上面的每一层砖块往下落一层
        for (int i = 0; i < bricks.Count; i++)
        {
            Vector3 p = bricks[i].transform.position;
            p.y = p.y - 1;
            bricks[i].transform.position = p;
        }
        //第四步,销毁砖块
        Destroy(brick,2f);
        yield return null;
    }

标签:Balls,07,Fire,transform,bricks,砖块,using,brick,public
来源: https://www.cnblogs.com/raymondking123/p/11471190.html