unity2d水果忍者案例

✨超级水果忍者,文章如有误请指正,如果觉得对你有用,请点赞收藏关注一波,谢谢支持😘

Build分享

百度网盘:
链接:https://pan.baidu.com/s/15HAgyTcvGHXonIyGEUJ6eg
提取码:lngk

提示:以下是本篇文章正文内容

效果图

请添加图片描述

代码

创建水果

public static FruitsOF_Create Instace;
//水果
public GameObject[] Fruits;
public GameObject Bomb;
private GameObject LivingFruits;

//判断难度
public bool isFruits;


//开始创建水果或炸弹
private bool isCreateFruits=false;


//创建水果的随机x与y轴
private float x;
private float z;

//几秒创建水果
private float timer = 2f;

//质量以及方向
private float Mass;
private Vector3 Direction;
private float MassSpeed = -0.3f;


void Start()
{
    Instace = this;
}

void Update()
{
    //判断难度关卡
    if (isFruits)
    {
        timer -= Time.deltaTime;
        if (timer < 0&&!isCreateFruits)
        {
            timer = 0f;
            isCreateFruits = true;
        }
        if (isCreateFruits)
        {
            //关卡的时间
            LvTime();
        }

    }
 

}

//设置关卡的速度
void LvTime()
{
    if (isFruits)
    {
        if (LV_Game.index == 0)
        {
            if (timer < 0)
            {
                LvDiffculty();
                timer = 2.5f;
            }
        }
        else if (LV_Game.index == 1)
        {
            if (timer < 0)
            {
                LvDiffculty();
                timer = 2.0f;
            }
        }
        else if (LV_Game.index == 2)
        {
            if (timer < 0)
            {
                LvDiffculty();
                timer = 1.5f;
            }
        }
        else if (LV_Game.index == 3)
        {
            if (timer < 0)
            {
                LvDiffculty();
                timer = 0.6f;
            }
        }
    }
}

//设置关卡难度
void LvDiffculty()
{
    if (LV_Game.time == 120&&LV_Game.index==0)
    {
        CreateFruits(true);
        Invoke("CreateFruits",0.8f);
        CreateFruits(true);
        if(Random.Range(1, 5) < 1.2f)CreateFruits(false);

    }
    else if (LV_Game.time == 100 && LV_Game.index == 1)
    {
        CreateFruits(true);
        Invoke("CreateFruits",0.6f);
        CreateFruits(false);
        if (Random.Range(1, 5) < 2.2f) CreateFruits(false);
    }
    else if (LV_Game.time == 60&&LV_Game.index==2)
    {
        CreateFruits(true);
        Invoke("CreateFruits", 0.4f);
        CreateFruits(true);
        Invoke("CreateFruits", 0.6f);
        CreateFruits(true);
        Invoke("CreateFruits", 0.8f);
        CreateFruits(true);
        Invoke("CreateFruits", 1f);
        CreateFruits(false);
        if (Random.Range(1, 5) < 3.3f) CreateFruits(false);
        if (Random.Range(1, 5) < 3.3f) CreateFruits(false);

    }
    else if (LV_Game.time == 40 && LV_Game.index == 3)
    {
        CreateFruits(true);
        Invoke("CreateFruits", 0.3f);
        CreateFruits(true);
        Invoke("CreateFruits", 0.4f);
        CreateFruits(true);
        Invoke("CreateFruits", 0.6f);
        CreateFruits(true);
        Invoke("CreateFruits", 0.8f);
        CreateFruits(false);
        Invoke("CreateFruits", 1f);
        CreateFruits(false);
        if (Random.Range(1, 5) < 4.4f) CreateFruits(false);
        if (Random.Range(1, 5) < 4.4f) CreateFruits(false);

    }
}


/// <summary>
/// 创建水果
/// </summary>
void CreateFruits(bool isCreateFruits)
{
    x = Random.Range(-2.6f,2.6f);
    z = Random.Range( -7f, - 7.1f);
    if (isCreateFruits)
    {
        LivingFruits = 
            Instantiate
            (Fruits[Random.Range(0, Fruits.Length)], 
            transform.position+new Vector3(x,-1,z), 
            Random.rotation);
    }
    else
    {
        LivingFruits = Instantiate(Bomb, transform.position + new Vector3(x, -1, z), Random.rotation);
    }
    Mass = Random.Range(1.5f, 1.8f) * -Physics.gravity.y *1.5f* MassSpeed;
    //方向
    Direction = new Vector3(-x * 0.1f * Random.Range(0.1f, 0.2f), -1,0);

    LivingFruits.GetComponent<Rigidbody>().velocity = Mass * Direction;

    LivingFruits.GetComponent<Rigidbody>().AddTorque(Random.Range(0, 10), Random.Range(0, 5), Random.Range(3, 5));

    isCreateFruits = false;
}

时间

public static StartTimer Instace;

//保存的时间
private float recordTime;
//总共多少时间
public float allTimer;
//记录开始的时间
private float startTime;
//显示的时间
private float showTime=LV_Game.time;

private bool curTime;

//是否停止时间
private bool isPauseTimer=false;
//是否开始时间
private bool isPlayTimer=false;


//时间转换
private int minute;
private int second;
private int grade;
//处理时间
private string str = string.Empty;
//计算使用了多长时间
private string useTime = string.Empty;
//用展示用来多长时间
public string useStr;

//展示时间
public Text TimerText;

void Start()
{
       Instace = this;
    StartCountingFun();
}

public void StartTimerFun()
{
    curTime = true;
    isPlayTimer = true;
    startTime = Time.time;
}

void Update()
{
    if (isPauseTimer) return;

    if (curTime)
    {
        recordTime = Time.time - startTime;
    }

    if (isPlayTimer)
    {
        showTime = allTimer - recordTime;

        useStr=useTimeS(showTime);

        if (showTime < 0)
        {
            //显示结束页面
            UILogic.Instace.showOvePanel();
            UILogic.Instace.OverScoreS(Line.Instace.score);
            UILogic.Instace.OverTimeS(useStr);
            isPauseTimer = true;
            return;
        }
        StartCountingFun();
    }

}
//开始计算的时间
void StartCountingFun()
{
    minute = (int)showTime / 60;
    second = (int)showTime % 60;
    grade = (int)(showTime * 100)%100;
    str = string.Format("{0:00}:{1:00}:{2:00}", minute, second, grade);
    
    TimerText.text = "倒计时:" + str;
}

//计算使用了多长时间
private string useTimeS(float showTime)
{
    minute = (int)(allTimer - showTime) / 60;
    second = (int)(allTimer - showTime) % 60;
    grade = (int)((allTimer - showTime) * 100) % 100;

    useTime = string.Format("{0:00}:{1:00}:{2:00}", minute, second, grade);

    return useTime;
}

准备开始文字

第一个脚本
private Color color;

private float ColorSpeed=0.8f;
void Start()
{
    color = transform.GetComponent<Text>().color;
}

void Update()
{
    if (transform.gameObject.activeInHierarchy)
    {
        color.a -= Time.deltaTime * ColorSpeed;
        transform.GetComponent<Text>().color= color;
    }
}
第二个脚本
public static PrepaStart Instace;

public GameObject prepare;
public GameObject start;

private bool isStartTimer=true;

//开始放音乐
public AudioSource source;

//时间到才能点击设置按钮
public bool isSetPanel;

void Awake()
{
    GetComponent<StartTimer>().allTimer = LV_Game.time;
}
void Start()
{
    Instace = this;

    //同步时间ui
    UILogic.Instace.DifficultyImgTextS(LV_Game.LvName[LV_Game.index]);

    source = GameObject.FindWithTag("AudioBg").GetComponent<AudioSource>

    if (isStartTimer)
    {
        StartCoroutine(startTimer());
    }

}

IEnumerator startTimer()
{
    yield return new WaitForSeconds(1f);
    prepare.SetActive(true);
    yield return new WaitForSeconds(1.5f);
    prepare.SetActive(false);
    start.SetActive(true);
    yield return new WaitForSeconds(1.5f);
    start.SetActive(false);
    yield return new WaitForSeconds(1f);
    //游戏开始
    FruitsOF_Create.Instace.isFruits=true;
    StartTimer.Instace.StartTimerFun();
    source.Play();
    //准备时间到才能点击设置按钮
    isSetPanel = true;
    //可以画线了
    Line.Instace.isSettingOut = true;
}

画线

public static Line Instace;

//保存鼠标按下的值
private Vector3 curValue;
//开始和结束的位置
private Vector3 startPos;
private Vector3 endPos;

//碰到是否按下和松开
private bool isDown = false;
private bool isBefore=false;
private bool isLeftButton = false;
private bool isUp = false;

//储存鼠标经过位子的超级坐标
private Vector3[] Postion=new Vector3[10];

//画线用到的line组件
public LineRenderer lineRenderer;

//判断鼠标移动距离
private float z=10;

//判断是否监测水果
private float IsMonitorValue;

//画线记录点
private int lineDo;

//鼠标移动分毫出线
private float lineTime;

//判断该水果出那个粒子的潭水效果
private int particleWaterIndex;

//声音播放器
private AudioSource source;
public AudioClip[] clip;

//水滴
public GameObject[] particleWater;
public GameObject[] particleJet;
private Vector3 particlePos;

//分数
public int score;
private int scoreVlaue = 50000;

//分数预制体
private GameObject scoreObj;
private GameObject[] obj;
private Color col;

[Header("判断什么时可以画线")]
public bool isSettingOut = false;

void Start()
{
    Instace = this;

    source = GameObject.FindWithTag("AudioManage").GetComponent<AudioSource>();

    //分数展现
    UILogic.Instace.ScoreTextS(score);

    isSettingOut = false;
}

/// <summary>
/// 碰到物体
/// </summary>
void ComeUpObject(RaycastHit hit)
    {
        if (hit.collider.gameObject.tag != "not")
        {
            //直接遍历出声音连声
            for (int i = 0; i < clip.Length; i++)
            {
                source.clip = clip[i];
                source.Play();
            }

            //水片
            hit.collider.gameObject.GetComponent<FruitsDieBody>().CreateDieFruits();

            //删除水果
            DeleteFruits.Instace.deleteFruits(hit, 0.1f);

            if (hit.collider.gameObject.tag == "apple_a") particleWaterIndex = 0;
            if (hit.collider.gameObject.tag == "lemon_a") particleWaterIndex = 1;
            if (hit.collider.gameObject.tag == "watermelon_a") particleWaterIndex = 2;

            particlePos = hit.transform.position;
            //切到水果
            if (hit.collider.gameObject.tag != "bomb")
            {
                particlePos.z = -6.5f;
                //创建水滴
                Instantiate(particleWater[particleWaterIndex],
                particlePos,
                Quaternion.identity);
                particlePos.z = -6.7f;
                //创建水瀑
                Instantiate(particleJet[particleWaterIndex],
                particlePos,
                Quaternion.identity);
            }
            //切到炸弹
            if (hit.collider.gameObject.tag == "bomb")
            {
                particlePos.x+=0.3f;
                scoreObj = Instantiate(GameManager.Instace.gamaConfi.jianScore,
                    particlePos,
                    Quaternion.identity);
                col= scoreObj.GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 1);
                Destroy(scoreObj, 1f);

                //创建分数UI
                //分数减减
                score -= scoreVlaue*2;
            }
            else
            {
                particlePos.x += 0.3f;
                scoreObj=Instantiate(GameManager.Instace.gamaConfi.addScore,
                particlePos,
                Quaternion.identity);
                col = scoreObj.GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 1);
                Destroy(scoreObj, 1f);

                score += scoreVlaue;
            }
            
            score = score < 0 ? 0 : score;

            if (score <= 0)
            {
                //显示结束页面
                UILogic.Instace.showOvePanel();
                UILogic.Instace.OverScoreS(Line.Instace.score);
                UILogic.Instace.OverTimeS(StartTimer.Instace.useStr);
            }

            //分数展现
            UILogic.Instace.ScoreTextS(score);

            hit.collider.gameObject.tag = "not";
        }
    }

void Update()
{
    curValue.x = Input.mousePosition.x;
    curValue.y = Input.mousePosition.y;

    isLeftButton = false;
    isUp = false;

    isDown = Input.GetMouseButton(0);

    if (isDown && !isBefore) isLeftButton = true;
    if (!isDown && isBefore) isUp = true;
    isBefore = isDown;

    if (isSettingOut)
    {
        pressDown();
    }

    //设置线段的相应颜色 
    Color c1 = new Color(1, 1, 0, IsMonitorValue);
    Color c2 = new Color(1, 0, 0, IsMonitorValue);

    lineRenderer.startColor = c1;
    lineRenderer.endColor = c2;

    if (IsMonitorValue > 0) IsMonitorValue -= Time.deltaTime*4;

    //分数obj
    if (scoreObj != null)
    {
        scoreObj.transform.position += Vector3.up * Time.deltaTime;
        col.a -= 0.007f;
        scoreObj.GetComponent<SpriteRenderer>().color = col;

    }
}

/// <summary>
/// 鼠标按下/拖动
/// </summary>
void pressDown()
{
    //鼠标点击
    if (isLeftButton)
    {
        startPos = curValue;
        endPos = curValue;

        lineDo =0;
        settingOut();
        lineTime = 0.25f;
        sendLinePosion();
        IsMonitorValue = 1f ;
    }
    //鼠标拖动
    if (isDown)
    {
        startPos = curValue;
        var a = Camera.main.ScreenToWorldPoint(new Vector3(startPos.x, startPos.y,z));
        var b = Camera.main.ScreenToWorldPoint(new Vector3(endPos.x, endPos.y,z));
        if (Vector3.Distance(a, b) > 0.1f)
        {
            lineDo++;
            settingOut();
            lineTime = 0.1f;
            sendLinePosion();
            IsMonitorValue = 0.6f;
        }
        endPos = curValue;
    }
    //鼠标监测物体
    if (IsMonitorValue>0.5f)
    {
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < Postion.Length; j++)
            {
                Vector3 x = Camera.main.WorldToScreenPoint(Postion[i]);
                Vector3 x1 = Camera.main.WorldToScreenPoint(Postion[i+1]);

                Ray ray = Camera.main.ScreenPointToRay(Vector3.Lerp(x, x1, Postion.Length));

                RaycastHit hit;

                if (Physics.Raycast(ray, out hit, 100, 1 << LayerMask.NameToLayer("aim"))){
                    
                    ComeUpObject(hit);
                }
            }
        }
    }

    if (IsMonitorValue < 0) lineDo = 0;

    lineTime -= Time.deltaTime;

    if (lineTime < 0)
    {
        lineDo++;
        settingOut();
        lineTime = 0.1f;
    }

}

/// <summary>
/// 开始画线-
/// </summary>
void settingOut()
{
    if (lineDo<=9)
    {
        for (int i = lineDo; i <= 9; i++)
        {
            Postion[i]= Camera.main.ScreenToWorldPoint(new Vector3(curValue.x, curValue.y, z));
        }
    }
    else
    {
        for (int j = 0; j <= 8; j++)
        {
            Postion[j] = Postion[j + 1];
        }
        Postion[9]= Camera.main.ScreenToWorldPoint(new Vector3(curValue.x, curValue.y, z));
    }
}

/// <summary>
/// 将保存的点添加到line上
/// </summary>
private void sendLinePosion()
{
    int i=0;
    foreach (Vector3 item in Postion)
    {
        lineRenderer.SetPosition(i, item);
        i++;
    }
}

创建水果被切到的预制体

public static FruitsDieBody Instace;

public GameObject[] FruitsDie;
public GameObject FruitsDieGameObject;

private Rigidbody rig;

private bool isDie;

private float DieFruitsSpeed=1;

void Start()
{
    Instace = this;
}

//创建水果被切后的的身体
public void CreateDieFruits()
{
    if (isDie) return;

    foreach (GameObject item in FruitsDie)
    {
        FruitsDieGameObject = Instantiate(item, transform.position, Random.rotation);

        rig = FruitsDieGameObject.GetComponent<Rigidbody>();

        if (rig != null)
        {
            rig.velocity = Random.onUnitSphere + Vector3.up;
            rig.AddForce(Random.onUnitSphere * DieFruitsSpeed, ForceMode.Impulse);
        }
    }
    isDie = true;
}

删除被切到产生的水果

private ParticleSystem particle;

[System.Obsolete]
void Start()
{
    particle = GetComponent<ParticleSystem>();
    if (particle != null)
    {
        Destroy(particle.gameObject, particle.startLifetime);
    }

}

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

SYFStrive

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

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

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

打赏作者

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

抵扣说明:

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

余额充值