Search Unity

Creating a hole inside a plane

Discussion in 'Scripting' started by Shuan_Wallashi, Sep 19, 2016.

  1. Shuan_Wallashi

    Shuan_Wallashi

    Joined:
    Sep 19, 2016
    Posts:
    4
    Hi everybody,

    I know this question were asked so many times. But most of them are from 2010-2013. And many of them I dont get it.

    My question is, is it possible to create a whole inside (on) a plane where the "ball" falls through?
    Can you please explain it to me an a very easy and understandable way? My english is not my mother tongue, sorry for that.

    I attached a screenshot to show you what I mean. I just draw the hole in MSPaint. This game is from the tutorial "Roll-a-ball". Now I just want to play around and add other things to it. For example a hole where the ball falls down. The idea is, to have many planes below or above each other. So if you fall, you will either land on another plane or game over. Actually there are so many things which could be applied once I know how to create a hole inside a plane like shown in the picture I attached.

    Please dear programmers, I would appreciate it if you, of course, could help me and if you can also provide me examples, links or tutorials related to your suggestions or helps.


    Cheers and best regards
     

    Attached Files:

    • hole.jpg
      hole.jpg
      File size:
      21.7 KB
      Views:
      3,649
    awsapps likes this.
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    What you're going to be wanting to google for is "Create hole in mesh".

    Now, what you're asking to do is a bit hard. You need to be able to do two things:
    - edit a mesh at runtime
    - do the maths to figure out how to create a hole-shaped hole (ie. round).

    I found a video here that explains the first part. Check it out. In order to do this, you have to understand what a mesh is, and how it works. The docs offer a pretty good explanation
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Firstly, you're going to want a "mesh" (lots of vertices to work with to make the hole) not a "plane" (four vertices, nothing to work with to make anything).

    Simplest way of making custom meshes would be a mesh editor (3ds max, maya, blender etc.). Make lots of variant and instantiate them at random (and at different rotations for added combinations).

    If you want to dynamically modify a mesh during runtime... well that gets a little more tricky...


    edit: I stand corrected, apologies to the lowly unity primitive plane :p :D
     
    Last edited: Sep 19, 2016
    Shuan_Wallashi likes this.
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    The default Unity plane is actually a mesh with 121 verts and 200 tris! So it's a nice base for doing things like this, if you don't need too much detail.
     
    Shuan_Wallashi and LeftyRighty like this.
  5. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    The simplest solution I can think of is doing it with tiles. Make an n x n grid with flat tiles, replace one with a round hole tile, or just remove it. Boom, hole in the ground.

    Or do it the cool way and implement boolean mesh operations.
     
    Shuan_Wallashi likes this.
  6. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    You'll have to create a custom mesh in 3D drawing software such as Blender, Sketchup, Maya etc...

    Or, you can make the floor out of cubes and then disable a cube. However that will result in only square holes.
     
    Shuan_Wallashi likes this.
  7. Shuan_Wallashi

    Shuan_Wallashi

    Joined:
    Sep 19, 2016
    Posts:
    4
    Wow, thanks to you all for your comments. So let me summarize what you all suggested:

    1. The terms I should use to google is "creating hole in a mesh"

    2. The easiest way is to create custom mesh in a 3D drawing software. I assume the file type will be recognized in Unity? Further I assume that all 3D drawing software will save the drawings a file type which is for all the same? Or are there only some specific 3D Software which is recognized by Unity?

    3. If I want to do it directly with Unity Editor, it wont be that easy, right? It is called "editing mesh at runtime". and it seems There is a lot of scripting and math I have to apply?

    4.
    This one sounds interesting despite the fact I didnt get it completely. Does it mean, I can play with the verts and tris and remove some of them whenever I want? Is there any example for that? Is it possible that unity shows all the verts and tris and by clicking on one of them I can remove it? Or isnt it that easy?

    5.
    That tiles thing sounds very interesting. Can you give me more information about that, if you dnt mind?
    And about the boolean mesh operations. How to apply it in unity?


    Again thanks everybody. I hope I this will also help other people and therefore I asked further question to have a reliable thread to help as many as possible.
     
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    All major 3D programs output files in a format called .fbx, which Unity handles natively. Unity also handles some specific programs' files, eg. Blender's .blend files.

    Note that 3D programs are quite hard to use - their interfaces are made to be fast to use when you're already good at them, not to be beginner-friendly. Blender is the most popular free one, but be ready to spend quite a bit of time in tutorials if you want to make your own meshes, even relatively simple ones.

    You can mess with that mesh, yes. Note that if you don't first make a copy, you'll edit the mesh of all planes in your game. The tutorial I posted a link to was doing exactly this; showing how to make it possible to delete parts of the default plane (and other meshes) by clicking on it. You should give it a look!


    There was a thread about somebody doing a Unity implementation a while back, so it's definitely possible. You can google for it, I can't remember if they shared the code for free or if they sold it on the asset store.
     
  9. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Probably the easiest would be to start learning blender, since all you need is a simple hole in a plane. Might take an hour or so of tutorials, but you'll learn something useful. I'm too lazy for that...
     
    Shuan_Wallashi likes this.
  10. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Tiles: Make some prefabs that are a little larger than the ball, maybe have the ball have a diameter of 0.8, and the tiles be 1 x 1. Make them either a cube, or just a plane. Then place a butt-load of them in an grid, like a chess board. Then you'll have your basic level. For all the holes you want, just remove one of the tiles. This gives you a square hole, though, so if you want a round hole, you'll have to fire up a 3d modeling program and make a hole tile.

    I'd advise against thinking about doing boolean operations when you are even asking questions about these other things, no offense. It's very complicated to do, as it involves manipulating the meshes directly, and a bunch of math. If you want to check it out anyway, here's a link to a paper on it. There's also an extension in the asset store.
     
    Shuan_Wallashi likes this.
  11. Shuan_Wallashi

    Shuan_Wallashi

    Joined:
    Sep 19, 2016
    Posts:
    4
    The tutorial is at runtime. Its good for games where u want to dig a hole etc. Really interesting. But for my case, I want it to do in unity editor...


    Well, then I will give it a try and use Blenders. Can I then stretch it in unity?

    Its not necessary to be round. I would be glade if i could do any kind of hole inside the mesh.

    Yes, true! I should not think about that. But boolean operations just makes sense too me. If 2 objects overlapping, then I thought we can remove the part where they overlapping. Seems its not that easy, isnt it?


    At least I got my answers now. And know that its not easy and includes a lot of coding or math. The easiest way is doing it in a 3D Software. Its easy but pretty annoying coy everytime u want to change something u have to open your 3d software again and edit it.
     
    Last edited: Sep 20, 2016
  12. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Ooooh, I'm an idiot. I thought you wanted to create holes when balls hit a plane. Durr!

    You should definitely be using Blender or some other 3d program to make your level geometry then.
     
    DroidifyDevs and Shuan_Wallashi like this.
  13. Shuan_Wallashi

    Shuan_Wallashi

    Joined:
    Sep 19, 2016
    Posts:
    4
    Hell no, no I liked your link. Because this shows me how to do it in runtime for many games such as minecraft etc. But seems even professionals doing it with a 3D software so will I. Do u know if there is maybe an asset which can do that?
     
  14. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Again, instead of doing something overly complex on runtime or buying assets, just do a tutorial on Blender.
     
  15. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Minecraft doesn't do this at all. They use 3-dimensional grid of cubes. When you hack away at the blocks, they just delete the cube from the grid. Nothing involving manipulating meshes.
     
  16. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Exactly, but Minecraft uses cubes. This is about making a round hole...
     
  17. eradima

    eradima

    Joined:
    Jan 4, 2017
    Posts:
    1
    Not sure if this is exactly what you are looking for, but I wanted to add the same feature to the roll-a-ball game.

    The solution I came up with was:
    1. Create a new plane in the scene
    2. Set the Is Trigger on the new plane
    3. Create a new tag for the plane (I called it hole)
    4. In the playerController.cs file I made the following changes:
    void OnTriggerEnter(Collider other)
    {
    if (other.gameObject.CompareTag("Hole"))
    {
    this.GetComponent<Collider>().enabled = false;
    }
    }
    // disabling the collider on the object (in this case the ball) causes it to fall through all the planes

    The next step from here would be to destroy the ball once it drops below the plane and then end the game. (I haven't done this yet)

    Hope this helps.
     
    BOC_fan and BrBarry like this.
  18. Ithkul

    Ithkul

    Joined:
    Apr 8, 2012
    Posts:
    22
    You been most helpful @Baste. Now I can implement trap doors in my existing world by editing mesh at run time. Cheers!
     
  19. zerotech15

    zerotech15

    Joined:
    Jan 28, 2016
    Posts:
    94
    Last edited: Mar 14, 2017
  20. vanshTandon

    vanshTandon

    Joined:
    Apr 10, 2018
    Posts:
    9

    But how to re enable collider?
     
  21. roshan090

    roshan090

    Joined:
    Mar 20, 2018
    Posts:
    35
    Hey Hello my friend. Can you please tell me how you made this? i have to project to create a hole in mesh like you did. Please tell me how you did this and save me thanks!
     
  22. zerotech15

    zerotech15

    Joined:
    Jan 28, 2016
    Posts:
    94
    Hey man. I did it with the shader, just basic alpha cutout. (I dont have source code anymore unfortunately)
     
    roshan090 likes this.
  23. nathanaelkinnis

    nathanaelkinnis

    Joined:
    Sep 24, 2020
    Posts:
    4
    For anyone still struggling with this in 2023, I finally found an easy solution for this.
    You can use probuilder and subtract a hole in a mesh.
    Source that explains this better: