The c++ blueprint makes the character disappear when the camera gets close to it, preventing obstructing the view

Take ThirdPersonCharacter, which comes with Unreal 4

1. Material treatment

Open the material blueprint used by Unreal 4’s ThirdPersonCharacter

Disconnect the blueprint of the output node and add the following nodes

The goal is to open up transparency so that other components can read and write

2. Create a component

Blueprint for the way

Create an Actor Component and create two functions and two variables (because there are two materials used)

Two of the variables are of type Material Instance Dynamic

Fade_BeginPlay:

There is one input and no output input mesh is of type Primitive Component

The lines are messy just to make the screenshot clear and the parent in Create Dynamic Material Instance is the name of the two materials that are used

Fade_EventTick: two inputs and no output

Function implementation can refer to c++ code to understand see c++ comments

C + + way

Create the Actor Component of the c++ class

.h files

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Copyright: home to the Author: Note that the "Opacity" variable required by this function needs to be manually set in the material blueprint. *** is a c++ version of blueprint AC_FadeWhenCameraClose. Attempts to write this component in c++ have failed so far -7.27- success !!!! We are the champions !!!! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /


#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "AC_FadeWhenCameraIsClose.generated.h"


UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class PROJECT_63_API UAC_FadeWhenCameraIsClose : public UActorComponent
{
	GENERATED_BODY(a)public:	
	// Sets default values for this component's properties
	UAC_FadeWhenCameraIsClose(a);protected:
	// Called when the game starts
	virtual void BeginPlay(a) override;

public:	
	// Called every frame
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

	
	// Dynamic material
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category ="Nevermore|Material" )
	UMaterialInstanceDynamic* PlayerBodyMaterial;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Nevermore|Material")
	UMaterialInstanceDynamic* PlayerChestLogoMaterial;


	/* As with public attributes, use the macro UFUNCTION. UFUNCTION() is responsible for exposing C++ functions to the reflection system. The BlueprintCallable option exposes it to the blueprint virtual machine. Each function exposed to the blueprint needs a Category associated with it so that the right-click shortcut menu function works correctly. * /

	UFUNCTION(BlueprintCallable, Category = "Nevermore|BPFunc_Lib")
	virtual void Fade_BeginPlay(UPrimitiveComponent* mesh);

	UFUNCTION(BlueprintCallable, Category = "Nevermore|BPFunc_Lib")
	virtual void Fade_EventTick(FVector CameraLocation, FVector Location);
		
};

Copy the code

.cpp file

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Copyright: home to the Author: Note that the "Opacity" variable required by this function needs to be manually set in the material blueprint. *** is a c++ version of blueprint AC_FadeWhenCameraClose. Attempts to write this component in c++ have failed so far -7.27- success !!!! We are the champions !!!! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /

#include "AC_FadeWhenCameraIsClose.h"
#include "Components/PrimitiveComponent.h"
#include "Kismet/KismetMaterialLibrary.h"
#include "Kismet/KismetMathLibrary.h"
#include"Kismet/KismetStringLibrary.h"
#include"Kismet/KismetSystemLibrary.h"


// Sets default values for this component's properties
UAC_FadeWhenCameraIsClose::UAC_FadeWhenCameraIsClose()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = true;

	// ...
}


// Called when the game starts
void UAC_FadeWhenCameraIsClose::BeginPlay(a)
{
	Super::BeginPlay(a);// ...
	
}


// Called every frame
void UAC_FadeWhenCameraIsClose::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	// ...
}


/************************************************** @brief : Create two dynamic materials that replace the one used by the character itself, thus taking control of the character material. BeginPlay can call @author from the blueprint: Nevemore @input: Mesh :character @output: @time: 2021-07-27 @ad: Can use the array way will he made more universal, but with less than now, I'm too lazy to optimize the * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
// So far failed - success !!!!! We are the champions!!
void UAC_FadeWhenCameraIsClose::Fade_BeginPlay(UPrimitiveComponent* mesh) {

	
	// The dynamic material creation method here is different from blueprint, more general, is to obtain materials through numbers
	UMaterialInstanceDynamic* t1 = mesh->CreateDynamicMaterialInstance(0);
	UMaterialInstanceDynamic* t2 = mesh->CreateDynamicMaterialInstance(1);

	PlayerBodyMaterial = t1;
	PlayerChestLogoMaterial = t2;

	if (IsValid(mesh)) {
		// Rename the created dynamic material to character
		mesh->SetMaterial(0, PlayerBodyMaterial);
		mesh->SetMaterial(1, PlayerChestLogoMaterial); }}/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * @ brief: distance too close character stealth @ the author: Nevemore @input: CameraLocation: Location: Location of character @output: @time: 2021-07-27 @ad: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
void UAC_FadeWhenCameraIsClose::Fade_EventTick(FVector CameraLocation, FVector Location){
	// Calculate the difference between the two
	float t1= UKismetMathLibrary::Vector_Distance(CameraLocation, Location);
	// Returns a value mapped from one range to another
	// Take this code as an example
	// When t1 is between 50 and 90, a value of 0.3-1 is returned
	float t2= UKismetMathLibrary::MapRangeClamped(t1, 50.000000.90.000000.0.300000.1.000000);
	
	// Convert T2 to a string and print it to the screen
	FString t3= UKismetStringLibrary::Conv_FloatToString(t2);
	UKismetSystemLibrary::PrintString(this, t3, true.true.FLinearColor(1.000000.0.244451.0.435678.1.000000), 0.000000);

	// When a dynamic texture exists, change the variable named "Opacity" in the texture blueprint
	// Note that the "Opacity" variable needs to be set manually in the material blueprint, as shown in the Chinese annotations in the M_Male_Body blueprint
	if (IsValid(PlayerBodyMaterial) && IsValid(PlayerChestLogoMaterial)) {
		PlayerBodyMaterial->UMaterialInstanceDynamic::SetScalarParameterValue(FName(TEXT("Opacity")), t2);
		PlayerChestLogoMaterial->UMaterialInstanceDynamic::SetScalarParameterValue(FName(TEXT("Opacity")), t2); }}Copy the code

3. Invoke it in the blueprint

Install the component to use

And then call

Drag the Mesh and Follow Camera directly from components

4. Performance effects

Over a long distance

Too close