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. Dismiss Notice

Discussion Cannot Change the Size of the Hole in Mesh with Scripting

Discussion in 'Scripting' started by RahmatAli_Noob, Sep 27, 2023.

  1. RahmatAli_Noob

    RahmatAli_Noob

    Joined:
    Sep 13, 2022
    Posts:
    67
    Hello Everyone, I am making the Game about Hole,

    The Game Contains Hole in the Mesh and the Object fall from it. Like Hole.io game.
    I have achieved that moving the Hole in the Mesh by the Vertices and the Hole is Relocating or Moving according to My Desire.
    I am Moving the Hole By getting the vertices of the Hole in Mesh and then Move or relocate it in the Script given Below.
    There are 2 Issue's About which I need help.

    1- I cannot make the Hole Bigger or Smaller by the vertices of the Hole in mesh.
    2- I cannot add more vertices in the Hole of the mesh.

    Is there Anyone Who can help the Image and the Code for the mesh is Given Below

    Image:

    Code (CSharp):
    1.  
    2. public MeshFilter meshFilter;
    3.  
    4.     public MeshCollider meshCollider;
    5.  
    6.     public Vector2 moveLimits;
    7.  
    8.     //Hole vertices radius from the hole's center
    9.     public float radius;
    10.  
    11.     public Transform holeCenter;
    12.     public Transform rotatingCircle;
    13.  
    14.     public float moveSpeed;
    15.  
    16.     Mesh mesh;
    17.     List<int> holeVertices;
    18.     //hole vertices offsets from hole center
    19.     List<Vector3> offsets;
    20.     int holeVerticesCount;
    21.  
    22.     float x, y;
    23.     Vector3 touch,
    24.     targetPos;
    25.  
    26.  
    27.     void Start()
    28.     {
    29.         RotateCircleAnim();
    30.  
    31.         //Initializing lists
    32.         holeVertices = new List<int>();
    33.         offsets = new List<Vector3>();
    34.  
    35.         //get the meshFilter's mesh
    36.         mesh = meshFilter.mesh;
    37.  
    38.         //Find Hole vertices on the mesh
    39.         FindHoleVertices();
    40.     }
    41.  
    42.     void RotateCircleAnim()
    43.     {
    44.         rotatingCircle.DORotate(new Vector3(90f, 0f, -90f), .2f).SetEase(Ease.Linear).From(new Vector3(90f, 0f, 0f)).SetLoops(-1, LoopType.Incremental);
    45.     }
    46.  
    47.     void Update()
    48.     {
    49.         MoveHole();
    50.         //Update hole vertices
    51.         UpdateHoleVerticesPosition();
    52.  
    53.     }
    54.  
    55.     void MoveHole()
    56.     {
    57.         x = Input.GetAxis("Mouse X");
    58.         y = Input.GetAxis("Mouse Y");
    59.  
    60.         //lerp (smooth) movement
    61.         touch = Vector3.Lerp(holeCenter.position, holeCenter.position + new Vector3(x, 0f, y), moveSpeed * Time.deltaTime);
    62.  
    63.         targetPos = new Vector3(Mathf.Clamp(touch.x, -moveLimits.x, moveLimits.x), touch.y, Mathf.Clamp(touch.z, -moveLimits.y, moveLimits.y));
    64.  
    65.         holeCenter.position = targetPos;
    66.     }
    67.  
    68.     void UpdateHoleVerticesPosition()
    69.     {
    70.         //FindHoleVertices();
    71.         //Move hole vertices
    72.         Vector3[] vertices = mesh.vertices;
    73.  
    74.         for (int i = 0; i < holeVerticesCount; i++)
    75.         {
    76.             vertices[holeVertices] = holeCenter.position + offsets;
    77.         }
    78.  
    79.         //update mesh vertices
    80.         mesh.vertices = vertices;
    81.         //update meshFilter's mesh
    82.         meshFilter.mesh = mesh;
    83.         //update collider
    84.         meshCollider.sharedMesh = mesh;
    85.     }
    86.  
    87.     void FindHoleVertices()
    88.     {
    89.         for (int i = 0; i < mesh.vertices.Length; i++)
    90.         {
    91.             //Calculate distance between holeCenter & each Vertex
    92.  
    93.             float distance = Vector3.Distance(holeCenter.position, mesh.vertices);
    94.        
    95.             if (distance < radius)
    96.             {
    97.                 //this vertex belongs to the Hole
    98.                 holeVertices.Add(i);
    99.                 //offset: how far the Vertex from the HoleCenter
    100.                 offsets.Add(mesh.vertices - holeCenter.position);
    101.             }
    102.         }
    103.  
    104.         //save hole vertices count
    105.         holeVerticesCount = holeVertices.Count;
    106.     }
    107.  
    108.  
    109.     //Visualize Hole vertices Radius in the Scene view
    110.     void OnDrawGizmos()
    111.     {
    112.         Gizmos.color = Color.yellow;
    113.         Gizmos.DrawWireSphere(holeCenter.position, radius);
    114.     }
    115.  
     
    Last edited: Sep 27, 2023
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
  3. RahmatAli_Noob

    RahmatAli_Noob

    Joined:
    Sep 13, 2022
    Posts:
    67
    Ok I will make sure to Do it Properly But Will you tell me How I can solve my issue's Thanks.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    I have no idea really beyond saying that I don't see how your approach can do what you want nor how it can be extended to do so. It's also not clear why you're doing iy i.e. why do you need to carve out a "hole" in an existing mesh.

    It seems rather a simplistic approach to something that can get pretty complex if you don't have the knowledge so saying that, you'd likely need to use a third-party library to do CSG (Constructive Solid Geometry). There are likely free libraries around but certainly ones on the asset store that do it.

    Maybe someone else can give you some direct links to things.