Trying to trace for an attached component with weapon

ComposingCoder
Fri, 09 Mar 2018 15:27:07 +0000
World Creation

Hello,

I am fairly new with programming and am working on trying to get my weapon to return an enemies attached component. I am using a bounding box for the collision, which is definitely returning a hit result, however, I am unable to get the component that I have made that sets the character’s stats such as HP and Combat. I would appreciate any help on fixing this, as I mentioned, I am quite new to programming. I would prefer not to use BP to solve this. I have two functions handling this, the trace, and then the function that breaks the hit result to get the component and deal damage to the component. I am also using an Enum to set the type of weapon as well as a struct to set the weapon stats. My trace is as follows:

*// The trace:*
     // FArmoryStats is a struct set in UWeaponComponent that sets weapon type, damage, mesh, ID, etc.
**void UWeaponComponent::OnAttack(FArmoryStats WeaponType, UWeaponComponent* AttackingCharacter)**

{
	AFirst_RPGCharacter* Character = Cast<AFirst_RPGCharacter>(AttackingCharacter->GetOwner());

	if (bIsAttacking && (Character != nullptr)) // if the Character is attacking with this weapon component
	{
		switch (WeaponType.Type)
		{
			case EArmoryType::AT_Melee: // Weapon is a melee weapon
			{
                            // MeleeHitBox is a UBoxComponent to give me the dimensions for collision that I want
				FVector MeleeTraceStart = MeleeHitBox->GetComponentLocation();
				FVector MeleeTraceEnd = (MeleeTraceStart * MeleeHitBox->GetScaledBoxExtent());
				FCollisionShape MeleeBox = FCollisionShape::MakeBox(MeleeHitBox->GetScaledBoxExtent());
				ECollisionChannel MeleeTrace = ECC_GameTraceChannel1;
				FCollisionQueryParams Params;
				Params.AddIgnoredActor(GetOwner());

				bIsHitMelee = GetWorld()->SweepMultiByChannel(MeleeHits, MeleeTraceStart, MeleeTraceEnd, FQuat::Identity, MeleeTrace, MeleeBox, Params);
			}



*// The break hit result and deal damage:*

**void UWeaponComponent::GetHitMeleeTarget()**

{
	if (bIsHitMelee) // If there is a hit from the trace, this currently will return a hit, can even get the name of the actor
	{
		for (auto & Hit : MeleeHits)
		{
			UE_LOG(LogTemp, Warning, TEXT("Hit Found"))
			UCombat_Component* TargetCombatComp = Cast<UCombat_Component>(Hit.GetActor()->GetComponentByClass(TSubclassOf<UActorComponent>()));
                    // Have also used in cast (Hit.GetComponent()) as well as (Hit.Component.Get())
			if (TargetCombatComp != nullptr) // If there is a hit to the Combat_Component
			{
				TargetCombatComp->TakeDamage(Stats.Damage); //Deal Damage
				float DamagedHealth = TargetCombatComp->GetCurrentHealth();                           // Get Target's Health
				UE_LOG(LogTemp, Warning, TEXT("Target Health: %s"), &DamagedHealth);                      // Log Target's Health
				return;
			}
			else if (TargetCombatComp == nullptr)
			{
				UE_LOG(LogTemp, Warning, TEXT("No Combat Component"));
				return;
			}
		}
	}
	else if (!bIsHitMelee) { return; } // If there was no hit return
}

Currently, I am getting the log that there is no combat component and can return the name of the target hit so I know that I am definitely getting a hit. If anyone could please help with this, or has any tips on my coding in general, I would be most grateful.

Thanks,

11 posts - 1 participant