初始状态

靠近垃圾桶弹窗

实时显示数据

摆放错误

任务完成

实现方法:

设置四个垃圾桶为触发器,玩家一旦碰到触发器,则激活垃圾分类的对话框。

这个对话框是屏幕覆盖类型的对话框。

对话框上摆放16个对象,也就是待分类的垃圾。

每个对象打上标签,分为四组。

这些对象均绑定拖拽脚本,可以用鼠标操作。

然后,每个对象添加刚体和碰撞盒子。

设置刚体重力大小为0,碰撞检测设为持续,并冻结旋转。

然后碰撞盒子设置为触发器。

4个垃圾桶均添加碰撞盒并设为触发器,然后挂载垃圾桶脚本,在脚本设置页面设置对应的组标签。

设置空对象挂载UI管理脚本

实时计算分类的数据并更新在UI上

(命名为SortCal更合适)

代码

计数屏

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

public class FSort : MonoBehaviour
{
    //当前所需要分类的垃圾组别
    public static int curGroup = 1;
    //四个垃圾桶依次为厨余垃圾、可回收物、有害垃圾、其他垃圾
    //垃圾总数据
    int[] Grouptotal = { 0, 6, 6, 3, 1 };
    //初始数据
    public static int[] Group = { 0, 0, 0, 0, 0 };
    //提示文本
    public Text text;//正确

    //提示所需变量
    public static int right;
    public static int wrong;
    public static int left;


    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //实时刷新垃圾数据
        right = Group[1] + Group[2] + Group[3] + Group[4];
        left = 16 - wrong - right;
        text.text = "正确:" +right+"   "+ "错误:" + wrong+"   " + "剩余:" + left;
        if (left == 0 && wrong == 0)
        {
            text.text = "     任务完成     ";
            text.fontSize = 80;
        }
        else
        {
            text.fontSize = 40;
        }
    }
}

垃圾桶

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

public class FBin: MonoBehaviour
{
    public int GroupLabel;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        Debug.Log("垃圾进入垃圾桶");
        if (collision.tag == "Group1")
        {
            FSort.curGroup = 1;
            if (GroupLabel == FSort.curGroup)
            {
                FSort.Group[1]++;
            }
            else FSort.wrong++;
        }
        if (collision.tag == "Group2")
        {
            FSort.curGroup = 2;
            if (GroupLabel == FSort.curGroup)
            {
                FSort.Group[2]++;
            }
            else FSort.wrong++;
        }
        if (collision.tag == "Group3")
        {
            FSort.curGroup = 3;
            if (GroupLabel == FSort.curGroup)
            {
                FSort.Group[3]++;
            }
            else FSort.wrong++;
        }
        if (collision.tag == "Group4")
        {
            FSort.curGroup = 4;
            if (GroupLabel == FSort.curGroup)
            {
                FSort.Group[4]++;
            }
            else FSort.wrong++;
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        Debug.Log("重摆");
        if (collision.tag == "Group1")
        {
            FSort.curGroup = 1;
            if (GroupLabel == FSort.curGroup)
            {
                FSort.Group[1]--;
            }
            else FSort.wrong--;
        }
        if (collision.tag == "Group2")
        {
            FSort.curGroup = 2;
            if (GroupLabel == FSort.curGroup)
            {
                FSort.Group[2]--;
            }
            else FSort.wrong--;
        }
        if (collision.tag == "Group3")
        {
            FSort.curGroup = 3;
            if (GroupLabel == FSort.curGroup)
            {
                FSort.Group[3]--;
            }
            else FSort.wrong--;
        }
        if (collision.tag == "Group4")
        {
            FSort.curGroup = 4;
            if (GroupLabel == FSort.curGroup)
            {
                FSort.Group[4]--;
            }
            else FSort.wrong--;
        }
    }

    private void OnCollisionStay2D(Collision2D collision)//弃用此方案
    {
        Debug.Log("碰撞测试");
    }
}

玩家

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using UnityEngine;
using UnityEngine.UI;

public class FPlayreControl : MonoBehaviour
{
    public Rigidbody2D rb;//rbody
    public Animator anim;
    public float speed;
    public float jump;
    public LayerMask ground;
    public Collider2D coll;

    private bool isGround;//判断是否在地面
    public Text GroupText;
    public GameObject Frame;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void FixedUpdate()
    {
        Movement();
        Switchanim();
    }

    void Movement()
    {
        float hormove;
        hormove = Input.GetAxis("Horizontal");
        float facedirection = 1;//Input.GetAxisRaw("Horizontal");
        //角色移动
        if (hormove != 0)
        {
            rb.velocity = new Vector2(hormove * speed * Time.deltaTime, rb.velocity.y);
            anim.SetFloat("running", Mathf.Abs(facedirection));
        }
        if (facedirection != 0)
        {
            transform.localScale = new Vector3(facedirection, 1, 1);
        }

        //角色跳跃
        if (Input.GetButtonDown("Jump") && isGround)
        {
            rb.velocity = new Vector2(rb.velocity.x, jump * Time.deltaTime);
            isGround = false;//离地
            //anim.SetBool("Jumping", true);
        }
    }

    void Switchanim()
    {
        anim.SetBool("idle", false);
        if (anim.GetBool("Jumping"))
        {
            if (rb.velocity.y < 0)
            {
                anim.SetBool("Jumping", false);
            }
        }
        else if (coll.IsTouchingLayers(ground))
        {
            anim.SetBool("idle", true);
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        Debug.Log("人物靠近垃圾桶");
        Frame.SetActive(true);
    }

    private void OnTriggerExit2D(Collider2D collision)
    {

    }

    //脚下踩了东西,进入触发
    private void OnCollisionStay2D(Collision2D collision)
    {
        //如果踩的是地面
        if (collision.collider.tag == "Ground")
        {
            isGround = true;//触地
        }
    }
}

 

posted on 2020-09-26 19:20  海月CSDN  阅读(536)  评论(0编辑  收藏  举报