为什么我的射线播放不检测到任何团结的网格山脉?

发布于 2025-02-04 18:57:41 字数 4496 浏览 3 评论 0原文

我有一个代码,即IM创建一个凸面船体,但要对我的凸面中的每个不同脸部/三角形进行实例化(具有网状渲染器,网状滤镜和网格撞机组件),因为我需要每个颜色是不同的颜色,然后我想去基于键盘点击移动射线播(Raycast浏览了我要点击的点列表,Raycast的中心是我的凸船体的质心,并且方向是基于我在该列表中浏览的每个点)。

我的射线广播从未发现网格山脉!它检测到球体,但永远不会啮合,尝试了许多解决方案,例如在固定程序上绘制射线广播,而不是在更新时尝试将两个代码放在同一脚本中,并将所有三角形都放在同一对象中(认为这可能是检测到的问题非凸),但没有任何作用。我什至尝试制作一个不被Raycast忽略的新图层,但什么都没有。

我的登录创建代码:

void CreateMyMesh()
    {
        for (int i = 0; i < faces.Count; i++)
        {
            if (faces[i].points.Count == 3)
            {
                Vector3 DirectionnnOFTRI = Vector3.Cross(faces[i].points[1] - faces[i].points[0], faces[i].points[2] - faces[i].points[0]).normalized;

                Vector3 CentroidTRI = new Vector3((faces[i].points[0].x + faces[i].points[1].x + faces[i].points[2].x) / 3
                , (faces[i].points[0].y + faces[i].points[1].y + faces[i].points[2].y) / 3
                , (faces[i].points[0].z + faces[i].points[1].z + faces[i].points[2].z) / 3);
                float Sign = Vector3.Dot(DirectionnnOFTRI, centroid - CentroidTRI);
                if (Sign > 0)
                {
                    for (int j = 2; j >= 0; j--)
                    {
                        Vector3 v = new Vector3(faces[i].points[j].x, faces[i].points[j].y, faces[i].points[j].z);
                        int iWhich = isExist(v, tmpVec);
                        if (iWhich == -1)
                        {
                            tmpVec.Add(v);
                            tmpTriangles.Add(tmpVec.Count - 1);
                        }
                        else
                        {
                            tmpTriangles.Add(iWhich);
                        }
                    }
                }
                else
                {
                    for (int j = 0; j < 3; j++)
                    {
                        Vector3 v = new Vector3(faces[i].points[j].x, faces[i].points[j].y, faces[i].points[j].z);
                        int iWhich = isExist(v, tmpVec);
                        if (iWhich == -1)
                        {
                            tmpVec.Add(v);
                            tmpTriangles.Add(tmpVec.Count - 1);
                        }
                        else
                        {
                            tmpTriangles.Add(iWhich);
                        }
                    }
                }
            }
            else
            {
                print("Error : #of points in Face != 3");
            }
            GameObject newMeshObject = Instantiate(MeshObject, MeshObject.transform.position, Quaternion.identity);
            newMeshObject.name = "triangle number " + i;
            
            Mesh mesh = new Mesh();
            newMeshObject.GetComponent<MeshRenderer>().material.color = new Color(tmpVec[0].x, tmpVec[0].y, tmpVec[0].z, 1f);
            newMeshObject.GetComponent<MeshFilter>().mesh = mesh;
            newMeshObject.GetComponent<MeshCollider>().sharedMesh = mesh;
            mesh.Clear();
            mesh.vertices = tmpVec.ToArray();
            mesh.triangles = tmpTriangles.ToArray();
            mesh.RecalculateNormals();
            mesh.name = "triangle mesh number " + i;

            tmpVec.Clear();
            tmpTriangles.Clear();
        }

我的射线播放代码:

int ct = 0;
    public void Raycasttt()
    {
        if(Input.GetKeyDown(KeyCode.UpArrow))
        {
            if(ct<rayyy.Count-1)
            {
                ct++;   
            }
            else
            {
                ct=0;
            }
        }
        
        Debug.DrawRay(transform.position, (rayyy[ct]) * 10, Color.yellow);

        RaycastHit hit;
        Vector3 Direc = ((rayyy[ct] * 10) - transform.position ).normalized;
        
        if(Physics.Raycast(transform.position, Direc, out hit, Mathf.Infinity, 6))
        {
            Debug.Log("in raycast");
            if (hit.collider != null)
                {
                    Debug.DrawRay(transform.position, (rayyy[ct]) * 10 * hit.distance, Color.yellow);
                    Debug.Log("Did Hit");
                    Debug.Log("HitPosition = " + hit.collider.gameObject.transform.position);
                }
                Debug.Log(hit.collider.gameObject.name);
        }
    }

我实例化的网状对象:

I have a code where im creating a convex hull but instantiating a gameobject (has mesh renderer, mesh filter, and mesh collider components) for each different face/triangle in my convex because I need each to be a different color, I then want to move raycast based on keyboard click (the raycast goes through a list of points that i want to hit, the center of the raycast is the centroid of my convex hull and the direction is based on each point I go through in that list).

My raycast never detects mesh colliders! it detects spheres but never meshes, tried many solutions such as drawing the raycast on FixedUpdate rather that on Update, also tried putting both codes in the same script as well as making all triangles in the same object (thought it might be a problem with detecting non-convex) but nothing worked. I even tried making a new layer that is not ignored by raycast and still nothing.

My convex creating code:

void CreateMyMesh()
    {
        for (int i = 0; i < faces.Count; i++)
        {
            if (faces[i].points.Count == 3)
            {
                Vector3 DirectionnnOFTRI = Vector3.Cross(faces[i].points[1] - faces[i].points[0], faces[i].points[2] - faces[i].points[0]).normalized;

                Vector3 CentroidTRI = new Vector3((faces[i].points[0].x + faces[i].points[1].x + faces[i].points[2].x) / 3
                , (faces[i].points[0].y + faces[i].points[1].y + faces[i].points[2].y) / 3
                , (faces[i].points[0].z + faces[i].points[1].z + faces[i].points[2].z) / 3);
                float Sign = Vector3.Dot(DirectionnnOFTRI, centroid - CentroidTRI);
                if (Sign > 0)
                {
                    for (int j = 2; j >= 0; j--)
                    {
                        Vector3 v = new Vector3(faces[i].points[j].x, faces[i].points[j].y, faces[i].points[j].z);
                        int iWhich = isExist(v, tmpVec);
                        if (iWhich == -1)
                        {
                            tmpVec.Add(v);
                            tmpTriangles.Add(tmpVec.Count - 1);
                        }
                        else
                        {
                            tmpTriangles.Add(iWhich);
                        }
                    }
                }
                else
                {
                    for (int j = 0; j < 3; j++)
                    {
                        Vector3 v = new Vector3(faces[i].points[j].x, faces[i].points[j].y, faces[i].points[j].z);
                        int iWhich = isExist(v, tmpVec);
                        if (iWhich == -1)
                        {
                            tmpVec.Add(v);
                            tmpTriangles.Add(tmpVec.Count - 1);
                        }
                        else
                        {
                            tmpTriangles.Add(iWhich);
                        }
                    }
                }
            }
            else
            {
                print("Error : #of points in Face != 3");
            }
            GameObject newMeshObject = Instantiate(MeshObject, MeshObject.transform.position, Quaternion.identity);
            newMeshObject.name = "triangle number " + i;
            
            Mesh mesh = new Mesh();
            newMeshObject.GetComponent<MeshRenderer>().material.color = new Color(tmpVec[0].x, tmpVec[0].y, tmpVec[0].z, 1f);
            newMeshObject.GetComponent<MeshFilter>().mesh = mesh;
            newMeshObject.GetComponent<MeshCollider>().sharedMesh = mesh;
            mesh.Clear();
            mesh.vertices = tmpVec.ToArray();
            mesh.triangles = tmpTriangles.ToArray();
            mesh.RecalculateNormals();
            mesh.name = "triangle mesh number " + i;

            tmpVec.Clear();
            tmpTriangles.Clear();
        }

My raycast code:

int ct = 0;
    public void Raycasttt()
    {
        if(Input.GetKeyDown(KeyCode.UpArrow))
        {
            if(ct<rayyy.Count-1)
            {
                ct++;   
            }
            else
            {
                ct=0;
            }
        }
        
        Debug.DrawRay(transform.position, (rayyy[ct]) * 10, Color.yellow);

        RaycastHit hit;
        Vector3 Direc = ((rayyy[ct] * 10) - transform.position ).normalized;
        
        if(Physics.Raycast(transform.position, Direc, out hit, Mathf.Infinity, 6))
        {
            Debug.Log("in raycast");
            if (hit.collider != null)
                {
                    Debug.DrawRay(transform.position, (rayyy[ct]) * 10 * hit.distance, Color.yellow);
                    Debug.Log("Did Hit");
                    Debug.Log("HitPosition = " + hit.collider.gameObject.transform.position);
                }
                Debug.Log(hit.collider.gameObject.name);
        }
    }

My mesh object that I instantiate:
MeshObject

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暮凉 2025-02-11 18:57:41

我已经有一段时间没有使用Unity网格了,但是在您将verticies和Triangles添加到网格中后,也许会添加网格集团的共享效果。也许网站汇编器在分配时使用网格的verticies和三角形,而不是更新的值,则使用了更新的值。

I haven't worked with Unity meshes for a while but maybe add the sharedMesh of your MeshCollider after you added the verticies and triangles to your Mesh. Maybe the MeshCollider uses the verticies and triangles of the Mesh at the time of assignment and not the updated values if it changes overtime.

ㄟ。诗瑗 2025-02-11 18:57:41

据我所知,为了让射线播放捕获网格对撞机,需要将其标记为检查员中的“凸”。

否则,您可以通过脚本在运行时启用凸配置,但是Inspector选项总是更好,因为您可以查看对撞机是否匹配您的形状!

这可能是您的射线广播无法捕获的另一个原因!您的对撞机需要匹配对象的形状!这解释了为什么您可以捕获球体和盒子山脉,而不能捕获网状山脉(假设它们被标记为凸面),因为球形和盒子具有特定的形状,raycast碰撞和触发器。当然,请确保层和标签也合适!

As far as I am aware in order for the raycast to catch the mesh collider it needs to be marked as "Convex" in the inspector.

Otherwise you could enable the convex configuration in runtime through a script however the inspector option is always a bit better because you can then see if the collider matches your shape!

This could be another reason why your raycast cannot catch it! your collider needs to match the shape of your object! This explains why you can catch sphere and box colliders but not mesh colliders (assuming that they are marked as convex to begin with) since the sphere and box have specific shapes that the raycast collides and triggers. Of course make sure that the layers and tags are appropriate as well!

好听的两个字的网名 2025-02-11 18:57:41

确保您的围墙足够大。如果您可以从Gizmos启用,您可以检查它们的外观。

Make sure your colliders are big enough. You can check how they look if you enable from gizmos.

浅沫记忆 2025-02-11 18:57:41

在图像中,网格集成器组件的网格字段为空。这意味着没有网格可发生碰撞,因此Raycast不会报告任何内容。在这里放一个网眼,射线广播将击中它。您也可以将相同的网格放在MeshFilter组件中,以便它也可以看到。

In the image, the Mesh field of the MeshCollider component is empty. This means that there's no mesh to collide with, so the Raycast won't report anything. Put a mesh here, and the Raycast will hit it. You may also put the same mesh in the MeshFilter component so it will become visible as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文