I am looking to implement Tree Chopping mechanics for my survival game.
Requirements:
- When player uses its axe and does swipe animation, the tree gets chopped and cut is made incrementally (like a hole in the mesh)
- After certain amount of swipes the tree gets chopped at the cut and falls using Physics.
- The cut is made always at fixed height from the bottom point of the tree and during animation the player controller capsule adjusts so that the cut is made at the the given height.
- Similar kind of tree chopping system can be seen in The Forest game. Following is the screenshot:
My Approaches and Problems I am facing:
- Creating a Depth Mask Object at the Cut position:
- In this approach I place a rectangular mesh at the point of cut. I set its ColorMask to 0 making it invisible and setting its ZWrite to On so it writes to Depth Buffer. I set the render queue of the alpha mask to Geometry - 1
- I then add a rectangular mesh in the void created by the alpha mask and set its render queue to Geometry - 2 so that it is drawn before the Mask void is drawn.
- The problem with this approach is that Anything behind the tree is not rendered in the groove of the tree and directly the skybox color is displayed.
- Placing a Replacement Tree mesh
- Hiding the tree from terraindata and putting a new tree prefab (which is similar to the existing tree but has additional submesh of the groove) in place of the tree. Then gradually hide the groove outer face renderer to show the inner groove which is modeled in the Tree mesh.
- As the tree size changes the cut height from the ground changes creating problems as for each size I have to create new meshes with groove mesh near the trunk.
- Using Shader Clip function
- I know we can use clip function inside the shader of the tree to clip meshes or put some dark color at the cut.
- I don’t how to do this. I think the problem with this is then all the trees have a new shader and don’t know if Unity allows for changing the shader of the tree in the terrain system.
None of the above ideas work very well, some in fact just don’t work.I would be grateful if you can share any ideas about how I might achieve this kind of chopping system.