• unity 随机生成物体方法


    GameObject rty;
    GameObject qwe;
    bool flag = true;
    private void OnTriggerEnter2D(Collider2D collision)
    {
    switch (collision.gameObject.tag)
    {
    case “123”:
    if (hy)
    {
    hy = false;
    Destroy(rty);
    refreshSpecificFoodItem(qwe);
    }
    else
    {
    startFlag = false;
    GetComponent().SetPositionAndRotation(new Vector3(-1f, -7f, 0), new Quaternion());
    }
    break;
    case “Fm”:
    if (hy) break;
    rty= Instantiate(collision.gameObject, GetComponent().position + new Vector3(0.6f, 0.6f, 0), new Quaternion());
    Destroy(rty.GetComponent());
    collision.gameObject.SetActive(false);
    rty.name = “1111”;
    hy = true;
    qwe= collision.gameObject;
    break;

            case "pe":
              
                if (hy)
                {
                    honeyCount++;
                    hy = false;
                    Vector3 homePos = new Vector3(
                        Random.Range(-0.1f, 0.1f),
                        Random.Range(-0.1f, 0.1f),
                        );
                    Destroy(rty);
                    refreshSpecificFoodItem(qwe);
                }
                break;
        }
    }
    private void es()
    {
        foreach (GameObject foodItem in fs)
        {
            bool flag = true;
            while (flag)
            {
                // assign position
                foodItem.transform.position = new Vector3(UnityEngine.Random.Range(-1f, 1f), UnityEngine.Random.Range(-1f, 0f), 0);
                Collider2D[] ci = Physics2D.OverlapCircleAll(new Vector2(foodItem.transform.position.x,foodItem.transform.position.y), 4);
                if (ci.Length == 0)
                {
                    flag = false;
                }
            }
            foodItem.SetActive(true);
        }
    }
    
    private void recm(GameObject Im)
    {
        while (flag)
        {
       
            fm.transform.position = new Vector3(UnityEngine.Random.Range(-1f, 1f), UnityEngine.Random.Range(-1f, 1f), 0);
            Collider2D[] ci = Physics2D.OverlapCircleAll(new Vector2(foodItem.transform.position.x, foodItem.transform.position.y), 6);
            if (ci.Length == 0)
            {
                flag = false;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
  • 相关阅读:
    SpringMVC 源码学习 返回值处理
    测试开发路线大纲与总结
    动态内存分配【C语言】
    后台管理登录权限怎么实现的,token具体有什么作用
    mysql 主从复制 mysql版本5.7.35
    并发编程(多线程)-可见性问题
    关于相机的思考
    基于Python实现的吃豆人游戏设计
    Cholesterol-PEG-FITC,Fluorescein-PEG-CLS,胆固醇-聚乙二醇-荧光素供应
    蚓链数字化营销系统与数字资产的关系
  • 原文地址:https://blog.csdn.net/qq_42986916/article/details/125413263