Foliage overlap with static mesh or triggerbox

So I am trying to get some foliage collision going. I have successfully given the foliage I use a ‘foliage’ collision preset and I can collide/overlap with the foliage.

So my wish is to have a sphere with the “on component begin overlap” event and have the event fire for every foliage it overlaps with as its radius is increased.
And this is where the issue lies, the sphere overlaps only with the first foliage, and then no other. I get what the problem is, there’s this main foliage actor that these foliage instanced static meshes are attached to, so the ‘begin overlap’ only happens once with the actor even though the sphere overlaps with other foliages. So then, how do I solve this? How can I trigger an event for each overlapping foliage?

Maybe I’m using the term foliage wrong, with foliage I mean each individual instanced static mesh component.
Addendum: I’m only using blueprints.

So, since I can’t think of a way to get each instanced static mesh component from the actor that have the overlap event, I thought I could use the “get instances overlapping sphere” node from a custom foliage instanced static mesh component, where the sphere center would be said actor location.

This happens in the actor responsible for the overlap event that starts it all. It’s simple enough, when the sphere overlaps any object with a collision preset called “foliage”, cast to the instanced foliage actor and cast to a custom foliage instanced static mesh component, which has a function we are calling. We are passing a reference to the sphere for its location and the instanced foliage actor because it holds the component class we want to add a new instanced static mesh components for.

Note that this blueprint class is child of Foliage Instanced Static Mesh Component, and the static meshes has this one selected in the Foliage tab.
So the first thing we do, the actors overlap event was triggered, so we know there’s some foliage here it is overlapping with. So we check for instances by making a overlapping sphere at the same location of the overlap actor and set its radius.
For each result we then save the transform of the array element so we can know where to create the new instance. We then remove the instance corresponding to the array element.
We then add a new instance of the mesh we want with the saved transform

It somewhat works, we “swap” the foliage to chairs… some of them. Not all of them seems to get registered in the collision? At least that’s where I think things go wrong. Spawning new actors responsible for the overlap event will swap some more bushes to chairs. Any pointers, I really have no idea how to make this more reliable.
Excuse me if I got some terminology wrong, I’m not really a programmer and this foliage ordeal is quite confusing.

I was just working on the same problem. I think I have a solution for you. The problem with your blueprint is the ‘for each’ loop right before your ‘remove instance’. every time you remove an instance, the old index values are no longer valid. So instead, do one overlap to count the number of foliage to change, then to a ‘for loop’, doing another overlap on each pass. Grab index zero on each pass. Message me if you want to work together, I think we could help each other out.

Hello, I have since solved this problem I had, realizing that simply removing indexes does not work well, but a reply is still nice. I shall post my blueprint setup in case you find something useful from it. It’s a long sequence so fitting it all in one image makes it a bit of a mess. But hopefully the lines are still clear where they connect.


Note that in my case, this happens inside the foliage component.

  1. We get the instances overlapping sphere and save the returned array as a variable.
  2. Since removing a lower index will give the indexes above a lower index, we start by removing the highest int, so now as instances are removed and some instances indexes are changed, it does not matter since we work our way down the list.
    We get the highest int with the function “max of int array”, and we save the result in two variables so we don’t have to run it over and over.
  3. Before deleting an instance it may be of interest to get some of its properties for other use, such as spawning another type of instance in the same place or knowing where to spawn a particle effect. As I look at the nodes now I realize there is some redundancy.
    Thing to note is that the Instance Index should be that of Max Value.
  4. We remove the instance, where the instance index should be that of Max Value.
  5. We set the Item of the Int Array at Index “Index Of Max Value” to -1 so that it is not picked again as “the max of int”.

you were right about not needing to call overlap on every loop iteration. I didnt realize that the lower index values would still be valid if you start from the top. I ended up finding a way to get it done in c++. The only thing I’m doing in blueprints is setting the value of ‘arrayOfFoliageTypes’, because I cant seem to compile when including the procedural foliage headers.

Here’s my code if you’re interested.



for (UInstancedStaticMeshComponent* ism : arrayOfFoliageTypes) {
	TArray<int32> foliageIndexValues = ism->GetInstancesOverlappingSphere(rounded_loc_cm, radius, true);
	foliageIndexValues.Sort();
	Algo::Reverse(foliageIndexValues);
	for (const int32 foliageIndexValue : foliageIndexValues) {
		ism->RemoveInstance(foliageIndexValue);
	}
}