【问题标题】:Why am I getting an "Index out of bounds" error here? - Unity C#为什么我在这里收到“索引越界”错误? - 统一 C#
【发布时间】:2021-10-11 05:54:23
【问题描述】:

我收到以下代码的“索引超出范围”错误。我已经注释了发生错误的行。此代码附加到由一系列 .obj 文件组成的 Gameobject。它基本上在点击播放时按顺序切换到下一个连续的 .obj 文件,从而使其显示为动画。我正在尝试翻阅这些 .obj 文件,并且在 Raycasting 上,我正在尝试获取命中点的顶点和法线。任何帮助将不胜感激,谢谢。

void Update()
{
    // on clicking the left mouse button
    if (Input.GetMouseButtonDown(2))
    {
        // get the currently updated mesh
        meshRenderer = GetComponentInChildren<MeshRenderer>();
        meshCollider.sharedMesh = null;
        meshCollider.sharedMesh = mesh;

        // get mesh data
        vertices = mesh.vertices;
        normals = mesh.normals;
        triangles = mesh.triangles;

        // transform interpolated point from local to world space and set attached object to that position
        hitTransform = meshCollider.transform;

        //Ray cast done to identify point on human mesh(on mouse click) and assign sensor to the identified point
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
           
        if (meshCollider.Raycast(ray, out hit, 200.0f))
        {
            // store index of the triangle (base index = index*3) and barycentric coordinates of the hitpoint
            Debug.Log("hit");
            triBaseIndex = hit.triangleIndex * 3;
            baryCenter = hit.barycentricCoordinate;

            v0 = vertices[triangles[triBaseIndex]];
            v1 = vertices[triangles[triBaseIndex + 1]];
            v2 = vertices[triangles[triBaseIndex + 2]];

        }

        interpolated_Position_v = v0 * baryCenter.x + v1 * baryCenter.y + v2 * baryCenter.z;

        float length_1 = Vector3.Distance(v0, interpolated_Position_v);
        float length_2 = Vector3.Distance(v1, interpolated_Position_v);
        float length_3 = Vector3.Distance(v2, interpolated_Position_v);
        
        if((length_1 > length_2) && (length_1 > length_3))
        {
            refIndex = triBaseIndex;
        }
        else if((length_2 > length_1) && (length_2 > length_3))
        {
            refIndex = triBaseIndex + 1;
        }
        else if((length_3 > length_1) && (length_3 > length_2))
        {
            refIndex = triBaseIndex + 2;
        }
    }

    // get triangle vertices
    p0 = vertices[triangles[triBaseIndex]]; //**THE ERROR POPS UP ON THIS LINE**
    p1 = vertices[triangles[triBaseIndex + 1]];
    p2 = vertices[triangles[triBaseIndex + 2]];

    // get normals at triangles vertices
    n0 = normals[triangles[triBaseIndex]];
    n1 = normals[triangles[triBaseIndex + 1]];
    n2 = normals[triangles[triBaseIndex + 2]];

    // interpolate position using the barycentric coordinate of the hitpoint to get accurate position within the triangle
    interpolatedPosition = p0 * baryCenter.x + p1 * baryCenter.y + p2 * baryCenter.z;
    // interpolate normals using the barycentric coordinate of the hitpoint & normalize it
    interpolatedNormal = (n0 * baryCenter.x + n1 * baryCenter.y + n2 * baryCenter.z).normalized;
    interpolatedPositionInWorldSpace = hitTransform.TransformPoint(interpolatedPosition);
    // determining the normal of the 3 vertices and assigning 
    interpolatedNormalInWorldSpace = hitTransform.TransformPoint(interpolatedNormal);
    right_vector = vertices[triangles[refIndex]] - interpolatedPosition;

    objectToAttach.transform.position = interpolatedPositionInWorldSpace;

【问题讨论】:

  • 什么是triBaseIndex,什么是triangles[triBaseIndex],每个数组的长度是多少?如果您现在知道,请在运行时使用调试器检查它。
  • 在raycasting上获取mesh三角形的顶点与下面的代码基本相同 - Vector3 p0 = vertices[triangles[hit.triangleIndex * 3 + 0]]; Vector3 p1 = 顶点[三角形[hit.triangleIndex * 3 + 1]]; Vector3 p2 = vertices[triangles[hit.triangleIndex * 3 + 2]];

标签: c# unity3d indexoutofboundsexception


【解决方案1】:

只有在按下鼠标时才设置verticestriangles,只有在光线投射命中时才设置triBaseIndex。如果未按下鼠标,或者光线投射未命中,则无法知道它们的值是什么,至少从给定的示例中不知道。我猜想大部分代码只有在光线投射实际命中时才会运行,我不确定如果光线投射未命中它打算做什么..

【讨论】:

  • 即使保证命中,我仍然会收到错误消息。我正在尝试获取顶点和法线并在该位置附加另一个游戏对象。
  • @poojaB 你说vertices[triangles[triBaseIndex]] 导致了错误,如果光线投射命中你之前运行完全相同的代码而没有错误。除非你对失败的路线有误解。
  • @PoojaB 无论是按下鼠标还是进行光线投射,您是否都在附加游戏对象?因此,如果您没有按下鼠标,您是否只是将同一个对象一遍又一遍地附加到同一个位置?还是每帧添加一个新对象?
  • 所以我只是尝试打印以查看是否发生了光线投射,而我意识到这甚至没有发生。我只是在单击鼠标时附加,它是通过每一帧放置在相同位置的相同对象,直到如果我想更改位置,我再次单击网格。
猜你喜欢
  • 2016-08-26
  • 2017-05-07
  • 2013-06-06
  • 1970-01-01
  • 2015-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
相关资源
最近更新 更多