Download

Can't attach weapon to character

Hey all,
I’ve been trying to get this to work all day with no luck. I want to attach a weapon to my character after he picks it up.

My weapon class has the following constructor:



AWeapon::AWeapon(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	// Create the base collision sphere component and set it as the root
	BaseCollisionComponent = PCIP.CreateDefaultSubobject<USphereComponent>(this, TEXT("BaseSphereComponent"));
	RootComponent = BaseCollisionComponent;

	// Create and configure the mesh for the pick up and attach it to the root
	WeaponMesh = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("WeaponMesh"));
	WeaponMesh->SetSimulatePhysics(true);
	WeaponMesh->AttachTo(RootComponent);

	SetReplicates(true);
}


When the character overlaps the weapon I call EquipWeapon in which I’ve tried numerous ways to attach it to the hand of my character, the most recent being:



void AMainCharacter::EquipWeapon(AWeapon* WeaponToEquip)
{
	EquippedWeapon = WeaponToEquip;
	EquippedWeapon->AttachRootComponentToActor(this, TEXT("WeaponSocket"), EAttachLocation::SnapToTarget);
}


But the weapon just sits there and doesn’t do anything. Does anyone have a working example of how to do this? I’ve tried googling, but all the examples I’ve seen are from people who are having similar problems.

Ok, quick update. I found a couple of issues.

  1. I was capturing a begin overlap event when my character overlapped with the weapons. It appears that everytime I tried to attach the weapon this was getting retriggered and going into effectively an eternal loop.

Once I prevented this from happening I ran into the second problem which was that my character just started flying uncontrollably around the screen. The problem I found was:

  1. The collision settings on the weapon were conflicting with my character.

Once I turned the weapon’s collision off the character now correctly picks up the weapon and it attaches.

However, it’s attaching to the wrong spot, but at least I’m closer now.