C++ - Attach camera to bone/socket?

Hello,

My C++ character class is named “VRSoldier”.
My skeletal mesh for this class is named “Soldier_ru_01”.

I have multiple questions :

1)
I would like to attach my VR camera to the head of my skeletal mesh, with C++.
I haven’t found documentation less than 5 years old and deprecated…
My current constructor in VRSoldier.cpp is :

// Sets default values
AVRSoldier::AVRSoldier()
{
 	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	// On cherche le skeletal mesh qui sera utilisé dans notre conteneur pour l'instant vide
	static ConstructorHelpers::FObjectFinder<USkeletalMesh> SK_SoldierMesh(TEXT("SkeletalMesh'/Game/Soldier_ru_01/Meshes/Soldier_ru_01.Soldier_ru_01'"));
	// On assigne le skeletal mesh à notre conteneur
	SoldierMesh = GetMesh();
	SoldierMesh->SetSkeletalMesh(SK_SoldierMesh.Object);
	
	// Create a first person mesh component for the owning player.
	//SoldierMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Soldier_ru_01"));
	
	VRCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("VRCamera"));

	//Attach the VR camera to the SoldierMesh.
	VRCamera->AttachToComponent(SoldierMesh, FAttachmentTransformRules::SnapToTargetIncludingScale);
	VRCamera->bLockToHmd = 1;

	// Make sure the mesh casts shadows
	SoldierMesh->bCastDynamicShadow = true;
	SoldierMesh->CastShadow = true;

}

The declarations (public) in VRSoldier.h are :

        // Soldier mesh
	UPROPERTY(EditAnywhere, Category = Mesh)
		class USkeletalMeshComponent* SoldierMesh;


	// VR camera.
	UPROPERTY(VisibleAnywhere)
		class UCameraComponent* VRCamera;

So I want to attach the camera to the head bone, or to its child socket named “headSocket” that I made.
It is probably related to AttachToComponent() but I don’t know how to do it.

2)
Currently, the Camera should be a child of the mesh considering I wrote this :

VRCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("VRCamera"));

	//Attach the VR camera to the SoldierMesh.
	VRCamera->AttachToComponent(SoldierMesh, FAttachmentTransformRules::SnapToTargetIncludingScale);

But for some reason, when I open the Blueprint class derived from my VRSoldier C++ class, the VRCamera is at the same level as the mesh, instead of being a child?

3)
When I place my Blueprint class in the level, for an unknown reason the camera is far from the mesh & capsule, however by default in the blueprint editor it’s shown inside of the capsule, and I manually moved it to the eyes height…

Why am I able to move it with the mouse, by the way, considering I defined it in C++ so I can only set the scale in the blueprint editor right panel, nothing else?

4)
I am currently using AttachToComponent, but I have seen AttachTo being used in old versions. Is it deprecated now?

5)
I have seen an old deprecated C++ [tutorial][5] where Epic is placing the mesh as a child of the camera. However, all modern examples I saw had the camera as a child of the mesh. Which version is better considering I want to use a VR camera, and create an aimspace to get the body to react to the camera? I am using a full body to create a “true FPP” character, and I will later implement inversed kinematic to control the arms with the motioncontrollers.

6)
I have set VRCamera->bLockToHmd = 1; (= LockToHeadMountDisplay) in the character class constructor. So I expected this option to be non-accessible from the derived blueprint class editor, but it is still accessible. Why?

Note :

I am trying to do all of this in C++ in despite of the problems associated with hard-coding references to assets, because

  • I want to learn doing it in C++.
  • I hate spaghettis.
  • I wasn’t able to define a socket for my camera considering it is Inherited, because I added it in the C++ class instead of adding it in the blueprint editor.
  • I don’t know how to create a function in my character class to attach objects to the hand, if both the skeletal mesh and it’s bones/sockets, and the MotionController component, are added later in the character blueprint. It would use references that do not exist…

About this Q&A :

I would like to know why Rama says

hardcoded asset references can break during packaging

He also explains that :

If you do indeed have a blueprint of the related class, you can just add the asset link in your .h file directly rather than hardcoding the lookup, which can and frequently does fail during packaging.

So this is the question 7) :
How can I add the asset link in my .h file directly? I suppose it’s by copying the reference SkeletalMesh'/Game/Soldier_ru_01/Meshes/Soldier_ru_01.Soldier_ru_01' ?
Then, how should I add it in the header? With an include?
Does it allow me to then work with this skeletal mesh, and remove :

static ConstructorHelpers::FObjectFinder<USkeletalMesh> SK_SoldierMesh(TEXT("SkeletalMesh'/Game/Soldier_ru_01/Meshes/Soldier_ru_01.Soldier_ru_01'"));

?

To resolve the question 2) , should I use TSubclassOf instead of AttachToComponent?

Hello,

I solved the questions 2) & 3) by replacing VRCamera->AttachToComponent(SoldierMesh, FAttachmentTransformRules::SnapToTargetNotIncludingScale); by VRCamera->SetupAttachment(SoldierMesh);.

Now the hierarchy is good, and the camera is not far away from the character when I place the blueprint in the level.

300939-hierarchy.png

300950-camera.png

I think I solved the question 1) by writing VRCamera->SetupAttachment(SoldierMesh, TEXT("headSocket"));.
I think it is solved but I am not sure because my camera’s origin has been moved to the head after compiling, however when I create a blueprint from this C++ class the blueprint editor does not show this camera is fixed to a socket…

The remaining questions are 4, 5, 6, 7.

4) Yes, AttachTo is deprecated.

Salut, j’ai remarqué que tu étais francais. Pourrais tu m’apporter de l’aide pour mon projet d’études ?
Actuellement je dois m’occuper de paramétrer la camera en mode fps ?
J’espere que tu repondras

De quoi as tu besoin?

Besoin d’aide afin de choisir comment et où m’orienter pour avoir un résultat. Je suis nouveau donc j’ai besoin dapprendre