Download

Weapon not attatching to Socket(C++).

Hi I’m new to Unreal engine and I’m moving from Unity. My goal is to make a simple third / first person shooter to get the hang of Unreal engines workflow and learn how Unreal does things. I got a few years of programming experience so I read the documentation and got a simple third person character up and running. I’m stuck on getting my weapon to attach to my characters left hand. It attaches to the bottom of the character and follows him. Been looking around the internet and can only find tutorials for blueprints for the most part. In Unity all I would have to do is parent a mesh to a joint either in Maya or in engine and swap out the mesh when needed. I think I did this correctly for the most part in Unreal. I checked and I do have a Socket called “SocketWeapon_1” in the assigned blueprint animation being used by the character. Am I assigning it to the wrong mesh???

Thanks for your help and time.

Skeleton Tree.png

Code


ACharacterBase::ACharacterBase()
{
	TurnRate = 45.0f;
	LookRate = 45.0f;
	WeaponNotDrawedArmLength = 140;
	WeaponDrawedArmLength = 110;
	AimingArmLength = 70;


	GetCapsuleComponent()->InitCapsuleSize(42.0f, 96.0f);

	bUseControllerRotationPitch = false;
	bUseControllerRotationYaw = false;
	bUseControllerRotationRoll = false;

	CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
	CameraBoom->AttachTo(RootComponent);
	CameraBoom->TargetArmLength = WeaponNotDrawedArmLength;
	CameraBoom->SocketOffset =* new FVector(0, 40, 50);
	CameraBoom->bUsePawnControlRotation = true;
	CameraBoom->bInheritPitch = true;
	CameraBoom->bInheritYaw = true;
	CameraBoom->bInheritRoll = true;

	FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
	FollowCamera->AttachTo(CameraBoom, USpringArmComponent::SocketName);
	FollowCamera->bUsePawnControlRotation = false;

	GetCharacterMovement()->bOrientRotationToMovement = true;	
	GetCharacterMovement()->RotationRate = FRotator(0.0f, 540.0f, 0.0f);
	GetCharacterMovement()->JumpZVelocity = 600.f;
	GetCharacterMovement()->AirControl = 0.2f;

//HERE IS WERE I ADD THE WEAPON IN CODE
	WeaponMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Weapon"));
	WeaponMesh->bCastDynamicShadow = false;
	WeaponMesh->CastShadow = false;
	WeaponMesh->AttachTo(Mesh, TEXT("SocketWeapon_1"), EAttachLocation::SnapToTargetIncludingScale, true);
	GunOffset = FVector(100.0f, 30.0f, 10.0f);

	AutoPossessPlayer = EAutoReceiveInput::Player0;

}

void ACharacterBase::BeginPlay()
{
	Super::BeginPlay();
}


void ACharacterBase::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
	Super::SetupPlayerInputComponent(InputComponent);

	InputComponent->BindAxis("LeftVerticalAxis", this, &ACharacterBase::MoveVertical);
	InputComponent->BindAxis("LeftHorizontalAxis", this, &ACharacterBase::MoveHorizontal);
	InputComponent->BindAxis("RightVerticalAxis", this, &APawn::AddControllerPitchInput);
	InputComponent->BindAxis("RightHorizontalAxis", this, &APawn::AddControllerYawInput);
	InputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
	InputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
	InputComponent->BindAction("Aim", IE_Pressed, this, &ACharacterBase::StartAiming);
	InputComponent->BindAction("Aim", IE_Released, this, &ACharacterBase::StopAiming);
}

void ACharacterBase::MoveVertical(float Value)
{
	if ((Controller != NULL) && (Value != 0.0f))
	{
		const FRotator Rotation = Controller->GetControlRotation();
		const FRotator YawRotation(0, Rotation.Yaw, 0);
		const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
		AddMovementInput(Direction, Value);
	}
}

void ACharacterBase::MoveHorizontal(float Value)
{
	if ((Controller != NULL) && (Value != 0.0f))
	{
		const FRotator Rotation = Controller->GetControlRotation();
		const FRotator YawRotation(0, Rotation.Yaw, 0);
		const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
		AddMovementInput(Direction, Value);
	}
}

void ACharacterBase::StartAiming()
{
	CameraBoom->TargetArmLength = AimingArmLength;
	bUseControllerRotationYaw = true;
}

void ACharacterBase::StopAiming()
{
	CameraBoom->TargetArmLength = WeaponDrawedArmLength;
	bUseControllerRotationYaw = false;
}

Your characters left hand socket was named SocketWeapon_l (L, not 1)

I use similar code:

  • Components are defined within the C++ Pawn class.
  • The static meshes that are attached to the components are edited in the blueprint subclass.
  • The static meshes are attached using sockets

The solution is to create the components in the constructor and then use the PostInitProperties() to connect the meshes together using:

[FONT=Lucida Console]Component->AttachTo(ParentComponent, TEXT(“socket_name”));

This works as PostInitProperties() is called after configuration has been loaded and the models assigned to the components in the blueprint code. At the time the constructor is called the models have not been loaded.