I am continuing the Battery Collector tutorial. This is the 14th video. Click here for the first part and here for the previous one.
Changing the Character’s Speed and Material
Code
In the BatteryCollectorCharacter.h file, I am adding 2 properties: SpeedFactor and BaseSpeed. I am also modifying the UPROPERTY for InitialPower and set it protected for Blueprint also.
/** The starting power level of our character */ UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Power", Meta = (BlueprintProtected = "true")) float InitialPower; /** Multiplier for character speed */ UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Power", Meta = (BlueprintProtected = "true")) float SpeedFactor; /** Speed when power level = 0 */ UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Power", Meta = (BlueprintProtected = "true")) float BaseSpeed;
In the BatteryCollectorCharacter.cpp, we define the default values for these properties in the construtor:
// set the dependent of the speed on the power level SpeedFactor = 0.75f; BaseSpeed = 10.0f;
I am modifying the UpdatePower function to set the MaxWalkSpeed of the Character based on his power:
// called whenever power is increased or decreased void ABatteryCollectorCharacter::UpdatePower(float PowerChange) { // change power CharacterPower = CharacterPower + PowerChange; // change speed based on power GetCharacterMovement()->MaxWalkSpeed = BaseSpeed + SpeedFactor * CharacterPower; }
In BatteryColectorCharacter.h, I am declaring a new function PowerChangeEffect() which can be implemented in blueprints.
UFUNCTION(BlueprintImplementableEvent, Caregory = "Power") void PowerChangeEffect();
I am just calling it in the UpdatePower function:
// change speed based on power GetCharacterMovement()->MaxWalkSpeed = BaseSpeed + SpeedFactor * CharacterPower; // call visual effect PowerChangeEffect();
In the Unreal editor, I am compiling the project.
Blueprint
I am opening the ThirdPersonCharacter blueprint. I am clicking on “open full blueprint editor”. In the viewport, I am selecting the mesh and in the Details Panel, I am opening the M_UE4Man_Body material. We can see that there is already a BodyColor parameter. Back to the ThirdPersonCharacter blueprint, I am modifying the Construction Script to define a Dynamic Material Instance as a PowerMaterial variable:
In the Event Graph, I am defining the Event Power Change Effect:
For the value, I am dividing the Current Power by the Initial value, clamping the result between 0 and 1 and lerp between 2 colors:
Then the result is:
I am compiling and saving. Then I am checking the result in play.
Next Step
Next, we will adding particles to the battery pickup.
Leave a Reply