# Trying to trace for an attached component with weapon

**URL:** <https://forums.unrealengine.com/t/trying-to-trace-for-an-attached-component-with-weapon/419343>\
**Category:** World Creation\
**Tags:** Trace, rpg, unreal-engine, melee, Collision, CPP, question\
**Created:** [March 9, 2018, 3:27pm UTC](https://forums.unrealengine.com/t/trying-to-trace-for-an-attached-component-with-weapon/419343 "2018-03-09T15:27:07Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ComposingCoder](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ComposingCoder](https://forums.unrealengine.com/u/ComposingCoder)\
**Post date:** [March 9, 2018, 3:27pm UTC](https://forums.unrealengine.com/t/trying-to-trace-for-an-attached-component-with-weapon/419343/1 "2018-03-09T15:27:07Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![Shaggy41](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shaggy41](https://forums.unrealengine.com/u/Shaggy41)\
**Post date:** [March 13, 2018, 8:08pm UTC](https://forums.unrealengine.com/t/trying-to-trace-for-an-attached-component-with-weapon/419343/2 "2018-03-13T20:08:49Z")

</div>

If I understand correctly:

You have a character who is holding a weapon. When the weapon hits an opponent, you want to change HP of this character. Also, you want to add some exp, points to the character that deals damage.

First, we have Soldier class.

**Soldier.h**

```
class MYPROJECT_API ASoldier : public ACharacter
{
	/** The weapon currently held in the hand by a soldier */
	class AWeapon * CurrentWeapon;
	
	void EquipWeapon(
		AWeapon * Weapon
	);
};

```

**Soldier.cpp**

```
void ASoldier::EquipWeapon(
	AWeapon * Weapon
)
{
	if (Weapon)
	{
		Weapon->SetOwner(this); // now the weapon will remember who holds it
	}
}

```

In Soldier we have a pointer to the currently held weapon. In EquipWeapon() we set the owner of the weapon. Now, if you want to get the weapon owner, use Weapon-\>GetOwner().

Next is Weapon class:

**Weapon.h**

```
class MYPROJECT_API AWeapon : public AActor
{
	/** Static Mesh */
	class UStaticMeshComponent * WeaponMesh;
	
	/** Event called when this Actor hit something
	UFUNCTION()
	void OnHit(
		UPrimitiveComponent * HitComponent,
		AActor * OtherActor,
		UPrimitiveComponent * OtherComp,
		FVector NormalImpulse,
		const FHitResult& Hit
	);
};

```

**Weapon.cpp**

```
AWeapon::AWeapon()
{
	// Here create WeaponMesh 
	
	// Set event when component hit something
	WeaponMesh->OnComponentHit.AddDynamic(this, &AWeapon::OnHit);
	
}

void AWeapon::OnHit(
	UPrimitiveComponent * HitComponent,
	AActor * OtherActor, // this is the Actor that weapon hit (enemy)
	UPrimitiveComponent * OtherComp,
	FVector NormalImpulse,
	const FHitResult& Hit
)
{
	if (OtherActor) //check if exist
	{
		if (OtherActor->GetClass()->IsChildOf<ASoldier>()) //check if the hit Actor is ASoldier
		{
			ASoldier * OtherSoldier = Cast<ASoldier>(OtherActor);
			
			// Now you can reduce HP of the enemy (for example 10 hp).
			OtherSoldier->ReceivedDamage(10.0f);
		}
	}
	
	if(GetOwner()) // check if owner of this actor exist. Owner is set in EquipWeapon() in ASoldier class.
	{
		ASoldier * MySoldier = Cast<ASoldier>(GetOwner());
		// Now you can add combo, points etc.
		MySoldier->AddCombo();
	}
}

```

In the constructor, we add our event. This event will be called every time when WeaponMesh hit in something. In OnHit() we have OtherActor representing the an actor who was hit by WeaponMesh. In GetOwner (), we have an actor who keeps this weapon.

To set the collision profile:

```
WeaponMesh->SetCollisionProfileName(FName("Weapon"));

```

You can create your own collision profile in Project Settings-\>Engine-\>Collision expand Present (sometimes you have to hide and expand, because the scrollbar does not appear).

 ![233654-collsion-profile.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/5/5/d55bbe0e0b71aaff0b33e68e1b09e10470fef5aa.png)

---

<div class="post-metadata">

**Author:** ![ComposingCoder](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ComposingCoder](https://forums.unrealengine.com/u/ComposingCoder)\
**Post date:** [March 14, 2018, 1:37am UTC](https://forums.unrealengine.com/t/trying-to-trace-for-an-attached-component-with-weapon/419343/3 "2018-03-14T01:37:54Z")

</div>

Thanks for your reply! So, what I have is a weapon that has a shape trace for collision, is this OnHit function better? I tried using it, but couldn’t get any return from it. Anyways, so I have the shape trace and am getting a hit. I also have a component that I made that handles stats like HP and sets whether the character is in combat. I am trying to get this combat component, in order to deal the damage. The actor itself has no Health stored on him. I will definately try OnHit again, but will I be able to return a component that is attached to the OtherActor? It’s base is UActorComponent.

---

<div class="post-metadata">

**Author:** ![Shaggy41](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shaggy41](https://forums.unrealengine.com/u/Shaggy41)\
**Post date:** [March 14, 2018, 8:15pm UTC](https://forums.unrealengine.com/t/trying-to-trace-for-an-attached-component-with-weapon/419343/4 "2018-03-14T20:15:46Z")

</div>

**[1]** Shape trace seems cool, but if you want using _OnHit()_ your mesh need have collision

 ![233776-collision-staticmesh.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/e/6/9e6544501a414a9d21e07c9f115f473a68dc6675.png)

and set in the constructor

```
WeaponMesh->SetNotifyRigidBodyCollision(true);

```

to your Mesh. [Here you can read more about collision][2]

**[2]** Here:

```
UCombat_Component* TargetCombatComp = Cast<UCombat_Component>(Hit.GetActor()->GetComponentByClass(TSubclassOf<UActorComponent>()));

```

I see you using _GetComponentByClass()_ so in your _AMyActor_ (_Hit.GetActor()_) you should add your _UCombat\_Component_ using _AddOwnedComponent()_.

---

<div class="post-metadata">

**Author:** ![Shaggy41](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shaggy41](https://forums.unrealengine.com/u/Shaggy41)\
**Post date:** [March 14, 2018, 8:18pm UTC](https://forums.unrealengine.com/t/trying-to-trace-for-an-attached-component-with-weapon/419343/5 "2018-03-14T20:18:26Z")

</div>

**[3]** This is my idea for a solution.

Every Actor has attached his own _UCombat\_Component_ where you store HP. So you can create a pointer to handle _CombatComponent_.

```
// MyActor.h
UCombat_Component * CombatHandle;

UCombat_Component * GetCombat();

// MyActor.cpp    
UCombat_Component * GetCombat()
{
    return CombatHandle;
}

```

In your _AMyActor_ class in the constructor, _BeginPlay()_ or somewhere you need to set _CombatHandle_ (I don’t know where you create _CombatComponent_).

Next in _UWeaponComponent::GetHitMeleeTarget()_ instead:

```
UCombat_Component* TargetCombatComp = Cast<UCombat_Component>(Hit.GetActor()->GetComponentByClass(TSubclassOf<UActorComponent>()));

```

use this:

```
// Cast to AMyActor to have access to GetCombat() function
AMyActor * HitActor = Cast<AMyActor>(Hit.GetActor()); 
UCombat_Component * TargetCombatComp = HitActor->GetCombat();

```

Now you should have access to _CombatComponent_.

[4] Your _UWeaponComponent_ inherits from _UActorComponent_, but you have also class _AWeapon_ inherits form _AActor_ to spawn in the world. [Here I read](https://docs.unrealengine.com/en-us/Programming/UnrealArchitecture/Actors?utm_source=editor&utm_medium=docs&utm_campaign=rich_tooltips)

> UActorComponent
> 
> This is the base Component. It can be  
> included as part of an Actor. It can  
> Tick if you want it to.  
> ActorComponents are associated with a  
> specific Actor, **but do not exist at  
> any specific place in the world**. They  
> are generally used for conceptual  
> functionality, like AI or interpreting  
> player input.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_c91def4e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_c91def4e](https://forums.unrealengine.com/u/anonymous_user_c91def4e)\
**Post date:** [March 14, 2018, 8:32pm UTC](https://forums.unrealengine.com/t/trying-to-trace-for-an-attached-component-with-weapon/419343/6 "2018-03-14T20:32:30Z")

</div>

The bool returned does not indicate a hit. It indicates whether something is blocking. IF it starts out blocking then there is no hit.

You should be iterating over the HitResults, and for any ‘valid’ hits processing the result. API’s like OnHIt() dont fire when they start out blocking. Its impossible to calculate an impact vector, or a hit normal. It is similarly impossible to depenetrate since this typically uses that information.

---

<div class="post-metadata">

**Author:** ![ComposingCoder](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ComposingCoder](https://forums.unrealengine.com/u/ComposingCoder)\
**Post date:** [March 14, 2018, 8:36pm UTC](https://forums.unrealengine.com/t/trying-to-trace-for-an-attached-component-with-weapon/419343/7 "2018-03-14T20:36:19Z")

</div>

Thank you again for your help. So the way I have set up my components that are attached (including the UCombat\_Component) is using an initialize function, ie. So I would just need to add a getter to return the Initialized component. Also, does OnHit work with Skeletal Mesh Components? My WeaponComponent is a USkeletalMeshComponent. I based it off of Ruben Ward’s Inventory Tutorial if you are familiar with that. As I said, lol, I’m new to programming. Thanks!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_c91def4e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_c91def4e](https://forums.unrealengine.com/u/anonymous_user_c91def4e)\
**Post date:** [March 14, 2018, 8:46pm UTC](https://forums.unrealengine.com/t/trying-to-trace-for-an-attached-component-with-weapon/419343/8 "2018-03-14T20:46:35Z")

</div>

Anything with a physics body is part of the physics scene, and can be tested against. This includes Skeletal Meshes, but does not have to. If you turn on Physics for the Skeletal mesh actor I believe you get a default capsule. But if you want to do something like cut an arm off or a leg, then you need more granularity than a capsule provides. You need a physics asset. Typically the physics bodies are authored in maya or which ever tool, at the same time as the mesh. Then its imported at the same time.

---

<div class="post-metadata">

**Author:** ![ComposingCoder](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ComposingCoder](https://forums.unrealengine.com/u/ComposingCoder)\
**Post date:** [March 14, 2018, 9:07pm UTC](https://forums.unrealengine.com/t/trying-to-trace-for-an-attached-component-with-weapon/419343/9 "2018-03-14T21:07:14Z")

</div>

I see, yeah I am using the free weapon assets from the Infinity Blade Packs, which don’t come with physics assets. Would that be why OnHit did not work? I did turn on overlap and hit events as well as enable physics. And OMG THANK YOU!!! While OnHit still is not working, making a getter function in my character that returns the initialized combat component and then retrieving it with your method worked!

---

<div class="post-metadata">

**Author:** ![ComposingCoder](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ComposingCoder](https://forums.unrealengine.com/u/ComposingCoder)\
**Post date:** [March 14, 2018, 9:09pm UTC](https://forums.unrealengine.com/t/trying-to-trace-for-an-attached-component-with-weapon/419343/10 "2018-03-14T21:09:46Z")

</div>

Thank you for your reply!

I am sorry, I am new to programming. Is my for (auto & Hit : MeleeHits) { //Do Stuff } not iterating?

---

<div class="post-metadata">

**Author:** ![ComposingCoder](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ComposingCoder](https://forums.unrealengine.com/u/ComposingCoder)\
**Post date:** [March 15, 2018, 3:53am UTC](https://forums.unrealengine.com/t/trying-to-trace-for-an-attached-component-with-weapon/419343/11 "2018-03-15T03:53:49Z")

</div>

Thanks again for your help! If you are interested, here’s a link to my video playlist showing what I have done on this game.

https://www.youtube.com/embed/l_T5LFoiKf4?feature=oembed&wmode=opaque&list=PL3f4VnKLzaAEN-ZwT_yKk1gY2sa4mXryY
