Boxing Game update 12 (31/01/24)


Today I wanted to make a block ability for my character and implement a way to manage the player’s state easily.

Player header file

UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input)
UInputAction* BlockAction;

FString PlayerState = "inactive";

void Block();

void LowerBlock();

void Damage(float DamageAmount);

In the header I setup some new values and functions. Firstly, “BlockAction” will be assigned to the “E” key and will have two related functions, “Block” and “LowerBlock”. Next, I established “PlayerState” which will be set whenever one of the key functions is carried out, but is “inactive” by default. Finally, I modified my “Damage” function to take a custom “DamageAmount” parameter for use in my block.

Player CPP file

void ACompetitionCharacter::OnOverlapStart(...)
{
if (OtherComp == (Enemy->LeftArmBox))
{
Damage();
if (PlayerState == "Blocking")
{
Damage(10.0f);
}
else
{
Damage(25.0f);

Here I’ve modified the calling of the “Damage” function so the player will take less damage when blocking, giving them an incentive to do so.

void ACompetitionCharacter::Block()
{
if (PlayerState == "Inactive")
{
PlayerState = "Blocking";
//Using the punching arm preset to show block is actieve
PunchingArm = NewObject<UStaticMeshComponent>(this, UStaticMeshComponent::StaticClass());
PunchingArm->SetStaticMesh(CylinderMesh);
PunchingArm->RegisterComponent();
PunchingArm->AttachToComponent(ACompetitionCharacter::GetRootComponent(), FAttachmentTransformRules::KeepRelativeTransform);
PunchingArm->CreationMethod = EComponentCreationMethod::Instance;
PunchingArm->SetRelativeTransform(FTransform(FQuat::Identity, FVector(60, 0, 0), FVector(0.4, 0.5, 0.2)));

During my block, the player arm component is used in a different position to show the player that the block is active. Also, at the start “PlayerState” is set, I’ve applied this to all functions and replaced “CanMove” with this variable.

void ACompetitionCharacter::LowerBlock()
{
if (PlayerState == "Blocking")
{
if (PunchingArm->WasRecentlyRendered())
{
PunchingArm->DestroyComponent();
}
PlayerState = "Inactive";

When block is lowered, “WasRecentlyRendered” detects if the blocking arm is still being rendered and destroys it accordingly.

void ACompetitionCharacter::ResetPunch()
...
EnhancedInputComponent->BindAction(BlockAction, ETriggerEvent::Started, this, &ACompetitionCharacter::Block);
EnhancedInputComponent->BindAction(BlockAction, ETriggerEvent::Completed, this, &ACompetitionCharacter::LowerBlock);

Here I setup the “ETriggerEvent” of the new input actions. When “E” is first pressed when initiating a hold, the block function is called and the block will stay up for the duration of the hold. When “E” is released, “LowerBlock” is called and the player will become susceptible to attacks again.


Leave a Reply

Your email address will not be published.