Boxing Game Update 6 (13/01/24)


Today I wanted to add player punching functionality:

Player Header

//InputAction to be set in blueprint
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input)
UInputAction* PunchAction;
...
//Indicates when arm component is valid
bool ArmActive = false;
//Static Mesh for arm
UStaticMeshComponent* PunchingArm;

I tried to find a variable or function that would tell me when the static mesh component wasn’t being rendered, but I was unsuccessful, so I used a simple boolean instead.

Player CPP file

void ACompetitionCharacter::Punch(const FInputActionValue& Value)
{
//Float that is positive(right) or negative(left)
const float WhichArm = Value.Get<float>();

//If Arm is already being rendered, destroy component and set to false
if (ArmActive == true)
{
PunchingArm->DestroyComponent();
ArmActive = false;
}
//if Arm isn't being rendered
if (ArmActive == false)
{
ArmActive = true;
if (Controller && (WhichArm != 0.f))
{
//Establish Punching arm component here
PunchingArm = NewObject<UStaticMeshComponent>(this, UStaticMeshComponent::StaticClass());
PunchingArm->SetStaticMesh(CylinderMesh);
PunchingArm->RegisterComponent();

//Attach to Root component
PunchingArm->AttachToComponent(ACompetitionCharacter::GetRootComponent(), FAttachmentTransformRules::KeepRelativeTransform);

PunchingArm->CreationMethod = EComponentCreationMethod::Instance;

//Places the arm where it should appear
PunchingArm->SetRelativeTransform(FTransform(FQuat::Identity, FVector(60, (WhichArm * 30), 0), FVector(1, 0.15, 0.095)));

The input action to punch is activated by a left or right click, right click is a positive float and left is negative. I use this to set the corrseponding location of the punching arm.

With how this function works, if the arm is already out it will be destroyed and create a new one on the appropriate side. If the arm is already on the right side this will function properly once I add player attacking collisions, as it will trigger a hit on overlap start when the component is destroyed and re-added.


Leave a Reply

Your email address will not be published.