首页 > 其他分享 >Unity如何判断两物体相交

Unity如何判断两物体相交

时间:2023-06-27 11:11:32浏览次数:31  
标签:cube 物体 float 相交 cubeA Unity GetComponent public size

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

public class cubemanger5 : MonoBehaviour
{
    public GameObject cubePrefab;
    public int numCubes;
    public float minSize;
    public float maxSize;
    public GameObject cubeA;
    public float moveSpeed;

    private List<GameObject> cubes = new List<GameObject>();
    private float totalSurfaceArea = 0f;

    // Start is called before the first frame update
    void Start()
    {
        // Instantiate cubes
        for (int i = 0; i < numCubes; i++)
        {
            GameObject cube = Instantiate(cubePrefab);
            cube.transform.position = new Vector3(Random.Range(-5f, 5f), Random.Range(0f, 5f), Random.Range(-5f, 5f));
            float size = Random.Range(minSize, maxSize);
            cube.transform.localScale = new Vector3(size, size, size);
            cube.GetComponent<Renderer>().material.color = Random.ColorHSV();
            cubes.Add(cube);

            // Calculate surface area
            totalSurfaceArea += 6 * size * size;
        }
    }

    // Update is called once per frame
    void Update()
    {  // Move cubeA
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");
        Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical);
        cubeA.transform.position += movement * moveSpeed * Time.deltaTime;
        if (Input.GetKeyDown(KeyCode.Space))
        {       

            // Check for collisions with other cubes
            foreach (GameObject cube in cubes)
            {
                if (cube != cubeA && cube.GetComponent<BoxCollider>().bounds.Intersects(cubeA.GetComponent<BoxCollider>().bounds))
                {
                    Debug.Log("相交");
                    // Calculate overlapping area
                    Vector3 overlap = Vector3.Scale(cubeA.GetComponent<BoxCollider>().bounds.size + cube.GetComponent<BoxCollider>().bounds.size - (cubeA.GetComponent<BoxCollider>().bounds.max - cube.GetComponent<BoxCollider>().bounds.min), new Vector3(1f, 0f, 1f));
                    float overlapArea = overlap.x * overlap.z;

                    // Subtract overlapping area from total surface area
                    totalSurfaceArea -= overlapArea;
                }
            }

            // Output total surface area
            Debug.Log("Total Surface Area: " + totalSurfaceArea);
        }
          
    }
}

 

标签:cube,物体,float,相交,cubeA,Unity,GetComponent,public,size
From: https://www.cnblogs.com/guangzhiruijie/p/17508146.html

相关文章

  • Unity3D:Hierarchy 窗口
    推荐:将NSDT场景编辑器加入你的3D工具链3D工具集:NSDT简石数字孪生Hierarchy窗口打开Unity新项目时的默认Hierarchy窗口视图Hierarchy 窗口显示场景中的每个游戏对象,如模型、摄像机或预制件。可以使用Hierarchy窗口对场景中使用的游戏对象进行排序和分组。在Scene视......
  • Unity计算表面积
    要计算在将一个新的长方体放入一堆紧密接触的长方体后,所有长方体的总表面积(去除与其他长方体的接触面积),可以按照以下步骤进行操作:创建一个脚本,并将其附加到包含一堆长方体的游戏对象上。在脚本中定义一个数组或集合来存储每个长方体的碰撞器组件。在适当的时候(例如,在新增长方......
  • Unity3D:扩展设备模拟器
    推荐:将NSDT场景编辑器加入你的3D工具链3D工具集:NSDT简石数字孪生扩展设备模拟器设备模拟器支持插件来扩展其功能并在模拟器视图中更改控制面板的UI。创建插件若要创建设备模拟器插件,请扩展设备模拟器插件类。若要将UI插入设备模拟器视图,插件必须:重写该属性以返回非空字......
  • Unity3D:扩展设备模拟器
    推荐:将NSDT场景编辑器加入你的3D工具链3D工具集:NSDT简石数字孪生扩展设备模拟器设备模拟器支持插件来扩展其功能并在模拟器视图中更改控制面板的UI。创建插件若要创建设备模拟器插件,请扩展设备模拟器插件类。若要将UI插入设备模拟器视图,插件必须:重写该属性以返回非空字......
  • unity3D:添加设备
    推荐:将NSDT场景编辑器加入你的3D工具链3D工具集:NSDT简石数字孪生添加设备若要将新设备添加到设备模拟器,请创建设备定义和设备覆盖。设备定义是Unity项目中扩展名的文本文件。它包含描述设备属性的JSON。.device设备覆盖是包含设备屏幕边框的图像,以及凹口、打孔和屏幕矩形......
  • Unity3D:模拟类
    推荐:将NSDT场景编辑器加入你的3D工具链3D工具集:NSDT简石数字孪生模拟类设备模拟器提供模拟类,可用于测试响应设备模拟器中特定于设备的行为的代码。以下模拟类位于UnityEngine.Device命名空间中:应用屏幕系统信息这些模拟类具有与其常规UnityEngine命名空间对应项相同......
  • 第三章 Unity Shader概述
    第三章UnityShader概述目录Shader和材质Shader的创建Shader的属性ShaderLabUnityShader的结构1.名字2.属性3.SubShaderShader的格式表面着色器顶点/片元着色器固定函数着色器着色器的选择Shader和材质Shader的创建标准表面着色器(StandardSurfaceShader):会产生一个......
  • unity物理系统简介
               FT=MV  FT:冲量  MV:动量   F:物体所受的力 T:力所持续的时间  M:物体的质量  V:速度                          铰链装在哪个轴,锚点参数......
  • visual studio 2022 community 多行编辑
    Alt+鼠标选择Alt+Shift+方向键Ctrl+Alt+鼠标选择Alt+Shift+;......
  • Unity3D:模拟器视图
    推荐:将NSDT场景编辑器加入你的3D工具链3D工具集:NSDT简石数字孪生模拟器视图“模拟器”视图在模拟的移动设备上显示应用程序。使用它来查看应用程序与该设备的屏幕形状、分辨率和方向的显示方式。模拟器视图的屏幕截图使用模拟器视图若要打开模拟器视图,请执行下列操作之一:......