Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Store Player Position and reycast it for deleting triangle return wrong triangle index

Discussion in 'Physics' started by dasio1986, Jun 8, 2020.

  1. dasio1986

    dasio1986

    Joined:
    May 25, 2020
    Posts:
    3
    Hi everyone,
    i'm new with unity and i'm programming a lot for learn but now i encountered a problem.
    I need to get the Triangle Index of specific mesh (in specific layer) for destroiyng it when a player walking on.
    i Try this code and Attach to the mesh i want to "destroy" but when the condition is triggered the deleted Triangles are not correct.
    What i do is start collect the position of character at colision enter and stop it at collision exit then i need to destroy all the triangles in the collected position.
    First code is very simple:
    I set a condition for update
    Code (CSharp):
    1. void OnCollisionEnter(Collision collision)
    2.     {
    3.         entered= true;
    4.      
    5.      
    6.     }
    After that in te Update function if entered is true i collect every Player Vector3 position:
    Code (CSharp):
    1.  
    2. if (entered)
    3.         {
    4.             if(last_position!= player.transform.position)
    5.             {
    6.                 last_position= player.transform.position;
    7.              
    8.              
    9.            
    10.                 position.Add(last_position);
    11.              
    12.             }
    13.          
    14. }
    Now in the exit collision event i do this:
    Code (CSharp):
    1.  entered= false;
    2.        
    3.         int layerMask = 1 << 8;
    4.      
    5.         layerMask = ~layerMask;
    6.         int j = 0;
    7.    
    8.  
    9.         RaycastHit hit;
    10.         Ray ray;
    11.         while (j < position.Count)
    12.         {
    13.          
    14.          
    15.             if (Physics.Raycast(position[j], Vector3.down, out hit, 100,layerMask))
    16.             {
    17.            
    18.                 delete_triangle(hit.triangleIndex);
    19.             }
    20.         }
    21.      
    22.  
    23.      
    24.         position.Clear();
    And this is the delete_triangle function where i receive an error "IndexOutOfRangeException: Index was outside the bounds of the array" at the below marked line and i notice that the Triangle Index is always -1
    Code (CSharp):
    1.      
    2. Destroy(this.gameObject.GetComponent<MeshCollider>());
    3.         mesh = GetComponent<MeshFilter>().mesh;
    4.         int[] old_trangle = mesh.triangles;
    5.         int[] new_triangle = new int[mesh.triangles.Length - 3];
    6.  
    7.         int i = 0;
    8.         int j = 0;
    9.         while (j < old_trangle.Length)
    10.         {
    11.             if (j != triangle_index * 3)
    12.             {
    13.                [COLOR=#ff0000] new_triangle [i++] = old_trangle[j++];[/COLOR]
    14.                 new_triangle [i++] = old_trangle[j++];
    15.                 new_triangle [i++] = old_trangle[j++];
    16.             }
    17.             else
    18.             {
    19.                 j += 3;
    20.             }
    21.  
    22.          
    23.         }
    24.  
    25.         this.gameObject.AddComponent<MeshCollider>();
    26.         transform.GetComponent<MeshFilter>().mesh.triangles = new_triangle ;
    Thank you in advance for your help
     
    Last edited: Jun 8, 2020
  2. dasio1986

    dasio1986

    Joined:
    May 25, 2020
    Posts:
    3
    So,
    i figure out how to do this but now i have another problem.
    The new setup is that i save all the position i want to reycast for find triangle to delete but the deleted triangles are wrong.
    The loop is:
    Code (CSharp):
    1. int layer_mask = LayerMask.GetMask("SafeZone");
    2.         RaycastHit hit;
    3.    
    4.           int k = 0;
    5.  
    6.         int layer_mask = LayerMask.GetMask("SafeZone");
    7.         while (k < position.Count)
    8.         {
    9.    
    10.             if (Physics.Raycast(position[k], -Vector3.up, out hit, Mathf.Infinity, layer_mask))
    11.             {
    12.               Debug.DrawRay(position[k], -Vector3.up * hit.distance, Color.yellow,100);
    13.                delete_triangle(hit.triangleIndex);
    14.             }
    15.             k++;
    16.         }
    17.         position.Clear();
    The delete_triangle function is:
    Code (CSharp):
    1.  
    2. void delete_triangle(int index)
    3.     {
    4.    
    5.         Destroy(safezone.gameObject.GetComponent<MeshCollider>());
    6.         Mesh mesh = safezone.transform.GetComponent<MeshFilter>().mesh;
    7.         mesh.MarkDynamic();
    8.    
    9.         int[] old_triangle = mesh.triangles;
    10.         int[] new_triangle = new int[mesh.triangles.Length-3];
    11.    
    12.         int i = 0;
    13.         int j = 0;
    14.    
    15.         while (j < old_triangle.Length)
    16.         {
    17.             if (j != index*3)
    18.             {
    19.            
    20.                 new_triangle[i++] = old_triangle[j++];
    21.                 new_triangle[i++] = old_triangle[j++];
    22.                 new_triangle[i++] = old_triangle[j++];
    23.             }
    24.             else
    25.             {
    26.                 j += 3;
    27.             }
    28.        
    29.         }
    30.         safezone.gameObject.layer = 8;
    31.         safezone.transform.GetComponent<MeshFilter>().mesh.triangles = new_triangle ;
    32.         safezone.gameObject.AddComponent<MeshCollider>();
    33.     }
    34.  

    Now for testing purpose i draw a line where the postion trigger the hit and as you will notice the new mesh is wrong:
    upload_2020-6-10_9-25-11.png
     
  3. dasio1986

    dasio1986

    Joined:
    May 25, 2020
    Posts:
    3
    Nobody can help me?