Boxing Game Update 7 (17/01/24)


For this update my goal was a camera that would move to the side the punch is thrown to, and reset along with punching arm after a timer elapsed. I also wanted to add player attacking collisions:

Player CPP file

ACompetitionCharacter::ACompetitionCharacter()
{
...
//Spring Arm is attached to root and camera is attached to spring arm
SpringArm = CreateDefaultSubobject<USpringArmComponent>(FName("SpringArm"));
SpringArm->AttachToComponent(ACompetitionCharacter::GetRootComponent(), FAttachmentTransformRules::KeepRelativeTransform);
Camera = CreateDefaultSubobject<UCameraComponent>(FName("Camera"));
Camera->AttachToComponent(SpringArm, FAttachmentTransformRules::KeepRelativeTransform);
//Looks like first person camera
SpringArm->TargetArmLength = 0.f;

I establish the spring arm and camera in initialisation and set the arm length to 0 to make a first person camera appearance.

void ACompetitionCharacter::OnOverlapStart(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
//If overlapping actor is the enemy....
if (ACompetitionEnemy* Enemy = Cast<ACompetitionEnemy>(OtherActor))
{
//if overlapping component is arm then apply damage
if (OverlappedComponent == (Enemy->LeftArmBox))
{
health = health - 25;
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, FString::SanitizeFloat(health));
}
}
//otherwise assume the player attacked and destroy enemy
else
{
Enemy->Destroy();

This manages collision between the player and enemy, I will change this later on to avoid false hits but for the time being this will suffice.

void ACompetitionCharacter::Punch(const FInputActionValue& Value)
{
...
//zoom the camera out
SpringArm->TargetArmLength = 100;
//change if the camera is on the left or right side by using "WhichArm"
SpringArm->SocketOffset = FVector(0, WhichArm * 100, 0);
...
//Sets a timer that executes the function "retreat" after 2 seconds
GetWorldTimerManager().SetTimer(PunchTimerHandle, this, &ACompetitionCharacter::Retreat, 2.0f, false);

This controls the cameras movement on the punch, pulling the camera out and moving it so the player can see their punch from the correct angle.

void ACompetitionCharacter::Retreat()
{
	//zoom the camera back in
	SpringArm->TargetArmLength = 0;
	//revert to first person
	SpringArm->SocketOffset = FVector(0, 0, 0);

	if (ArmActive == true)
	{
		PunchingArm->DestroyComponent();
		ArmActive = false;
	}
}

This code will reset the camera position to that of a first person view.


Leave a Reply

Your email address will not be published.