Search Unity

Need to disable and enable Collider after mesh.Refresh() ?

Discussion in 'World Building' started by ahkow, Oct 31, 2020.

  1. ahkow

    ahkow

    Joined:
    Aug 13, 2015
    Posts:
    11
    I'm trying the MeshEditorScene under "Assets/Samples/ProBuilder/4.4.0/Runtime Examples/Runtime Editing".
    1. I extrude the top face of the cube to make it higher.
    2. When I hover mouse over the top face, it is no longer highlighted. And I cannot extrude this top face again.
      upload_2020-10-31_17-39-42.png

    3. It looks like this is caused by collider not being updated after the mesh has changed.
    4. So I added code in MeshEditor.cs (Line 20 to 23) to "refresh" the collider. After this change, when I hover the mouse over the top face, it is highlighted and can be extruded again.
    Code (CSharp):
    1.  
    2.         void UpdateDrag()
    3.         {
    4.             var distance = GetDragDistance() - m_DragState.offset;
    5.  
    6.             var mesh = m_Selection.mesh;
    7.             var indices = m_DragState.meshState.indices;
    8.             var vertices = m_DragState.meshState.vertices;
    9.             var origins = m_DragState.meshState.origins;
    10.             // Constraint is in world coordinates, but we need model space when applying changes to mesh values.
    11.             var direction = mesh.transform.InverseTransformDirection(m_DragState.constraint.direction);
    12.  
    13.             for (int i = 0, c = indices.Count; i < c; i++)
    14.                 vertices[indices[i]] = origins[i] + direction * distance;
    15.  
    16.             mesh.positions = vertices;
    17.             mesh.ToMesh();
    18.             mesh.Refresh();
    19.  
    20.             //======= "Refresh" collider =======
    21.             var col = mesh.GetComponent<Collider>();
    22.             col.enabled = false;
    23.             col.enabled = true;
    24.         }
    25.  
    Question:
    Is this how it's supposed to behave? Do I really need to disable and enable the collider to make it work?



    Environment:
    * Unity 2019.4.13f1
    * ProBuilder 4.4.0
     
  2. ahkow

    ahkow

    Joined:
    Aug 13, 2015
    Posts:
    11
    Anyone knows the answer :) ? Is this a bug or I'm missing something?
     
  3. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    552
    If you look in the Utility class for that example you can see that it's using the physics collider for object picking. If you don't want to use a collider then you'll need to implement another method for picking the object. The good news here is that face, edge, and vertex picking don't rely on physics, so once you have an object you are good to go :)
     
  4. ahkow

    ahkow

    Joined:
    Aug 13, 2015
    Posts:
    11
    Thanks for the tips! I tried SelectionPicker.PickFacesInRect() and it could give me the object that was under cursor without using collider.
     
    Last edited: Nov 18, 2020
    kaarrrllll likes this.