Is this your tick function? If it is, then this looks really bogus.
You are loading a material instance every tick, which is already as slow as it gets. However you are also using the UMaterialInstance as UMaterialInstanceDynamic which is not correct. Try the CastChecked() instead of a plain c-style cast to make sure you can safely cast between the two types. So basically you are modifying the source asset (MaterialInstance) and not a dynamic instance of that material.
Instead you should create a dynamic material instance during initialization (e.g. BeginPlay), keep the pointer to it and then only set the distance parameter within your tick function.