Download

GetInstancesOverlappingSphere not getting my instance

I’m working with instanced static meshes and I’m having an issue with GetInstancesOverlappingSphere where its not always getting my instance even though the center of my sphere covers that area and intersects with the instance (drawing a debug sphere to confirm). Sometimes it picks it up usually around the edges but thats about it. You can see the results in the image.

int32 ABuilding::GetHitIndex(const FHitResult& HitResult)
{
    TArray<int32> HitIndexes = FoundationInstancedMesh->GetInstancesOverlappingSphere(HitResult.Location, 15.0f);
    DrawDebugSphere(GetWorld(), HitResult.Location, 5.0f, 10, FColor::Red);
 
    if (HitIndexes.Num())
    {
        return HitIndexes[0];
    }

    return -1;
}

Who is that within the function? Is it even valid?
And you should probably check it / return if not valid regardless of the fact it could actually be valid.

Check the function header too.

You may need that last worldspace parameter set - if it’s a thing in cpp.

Specially since the line after makes it look like the result vector is in fact a world location?

Ps:
No point in creating a variable to just return is there? If you were doing some math then yea…

Yes it is valid and its getting altered once I figure out what is causing this issue. It doesnt need the third parameter as it defaults to true which is the value I want so it is in world space by default. I edited the original post to make it a bit more clear as to what is happening. It works sometimes in some of the areas of the mesh, but its very unreliable except for the areas that it actually picks up the index.
“No point in creating a variable to just return is there? If you were doing some math then yea…”
The function will be doing more once I get this problem figured out and it keeps it cleaner. Its moreso of a helper than anything and will be of a decent size once finished.

Not sure how to be more clear

Unless you are acrually making sure that FoundationInstancedMesh is valid and has the correct value when you call this function - you cannot expect the function to work.

And I’m not sure how I can be more clear. Like I said I know FoundationInstancedMesh is valid and I know the parameters are correct and it also works only part of the time as stated in the OP. I even tried what you said to show you what I already stated above as it has the same result. The collision mesh is correct on the mesh as well and I looked at the bounds and their fine to. I also get the same result if I make the HitResult.Location relative to the FoundationInstancedMesh and set it to use relative space.

int32 ABuilding::GetHitIndex(const FHitResult& HitResult)
{
	if (FoundationInstancedMesh)
	{
		TArray<int32> HitIndexes = FoundationInstancedMesh->GetInstancesOverlappingSphere(HitResult.Location, 30.0f, true);
		DrawDebugSphere(GetWorld(), HitResult.Location, 30.0f, 10, FColor::Red);

		if (HitIndexes.Num())
		{
			return HitIndexes[0];
		}
	}
	return -1;
}

EDIT: Fixed using HitResult.Item, holds the hit instance.

For the future.

Just because a function works some of the time doesn’t mean it won’t cause a crash later on by accessing something that’s null.

The way you have it should work. At least in theory.
The better way would be to pass a reference in as that mesh variable.

HitResult.Item may contain the index, it could also not. You may need to check and make sure:
Is instance 0 a thing or is it bumped up to 1 so that 0 being returned means no hit? For instance.

Anyway, glad you sorted it.