Changing mesh filter component of prefab when other object stops overlapping
I am trying to create a VR game, where one part of the game consists of the player digging the soil to create a hole
I have created a dirt mound prefab, and in order to simulate the dirt getting reduced after the player digs it, I am currenlty thinking of doing this:
When the collider of the spade overlaps with the dirt mound, do nothing, i.e. the spade should be able to pass through the dir mound
When the collider of the spade stops overlapping with the dirt mound, change the mesh of the dirt mound to a model of the dirt mound that is reduced.
Currently, the mesh collider of the dirt mound is set as Convex = true, isTrigger = true
This is the code I have so far for the dirt moundpublic class Soil : MonoBehaviour { public Mesh hole_50; public Mesh hole_100; private float digging_state = 0; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } void OnTriggerEnter(Collider other) { // Do nothing } void OnTriggerExit(Collider other) { if (digging_state == 0f) { this.gameObject.GetComponent<MeshFilter>().mesh = hole_50; digging_state += 0.5f; Debug.Log("Digging state from 0 to " + digging_state); } if (digging_state == 0.5f) { this.gameObject.GetComponent<MeshFilter>().mesh = hole_100; digging_state += 0.5f; Debug.Log("Digging state from 0.5 to " + digging_state); } } }
Basically, I have 2 Mesh variables that keep track of the different meshes to load when the state of the dirt mound changes.
For state = 0, it means that the dirt mound is not dug yet
For state = 0.5, it means that the dirt mound is slightly dug
For state = 1.0, it means that the dirt mound is completely dug and there is a hole in it
Here is an image with the dirt mounds of different states
For some reason, when I test the code out, the moment the spade even touches the dirt mound, the dirt mound jumps to the last state with the hole in it, and I can't figure out why.
Is there any better way to simulate digging soil?
Answer by rh_galaxy · Mar 22, 2021 at 04:56 PM
digging_state will go from 0 to 1.0 the first time in OnTriggerExit().
When 0 you add 0.5, then if 0.5 it will become 1.0 because of:
if (digging_state == 0.5f)
Your answer
![](https://koobas.hobune.stream/wayback/20220613031121im_/https://answers.unity.com/themes/thub/images/avi.jpg)