Set Socket Location/Transform?

Hi,

I can’t find the way how to set a socket location or transform. We have GetSocketLocation. So how to Set? And how to set socket rotation?

EDIT:

The socket of my enemy’s hand should follow the spine bone/socket of my character.

USkeletalMeshSocket const * EnemyhandR = Enemy1->GetMesh()->GetSocketByName("hand_r");
USkeletalMeshSocket const * MHSpine3 = Enemy1->GetMesh()->GetSocketByName("spine_03");

FVector EnemySLoc = Enemy1->GetMesh()->GetSocketLocation("hand_r");
FVector MHSLoc = MH->GetMesh()->GetSocketLocation("spine_03");
FVector Forward = (MHSLoc - EnemySLoc);
FRotator SocketRot = FRotationMatrix::MakeFromX(Forward).Rotator();

And

EnemyhandR->SetSocketRotation(SocketRot);

But no SetSocketRotation.

Hey,

try to use:

EnemyhandR->RelativeLocation = FVector(); 
EnemyhandR->RelativeRotation = FRotator(); 
EnemyhandR->RelativeScale = FVector(); 

but GetSocketByName() return const value, so you cannot modify EnemyhandR (it is read only).

You may try one of this (I’m not sure if it will work the way you want):

[1] cast GetSocetByName() to non-const

USkeletalMeshSocket * EnemyhandR = const_cast<USkeletalMeshSocket*>(Cast<USkeletalMeshSocket>(Mesh->GetSocketByName("hand")));

[2] create a handler to USkeletalMesh

USkeletalMesh * SkeletalMeshHandle;

next

static ConstructorHelpers::FObjectFinder<USkeletalMesh> Finder_SkeletalMesh(TEXT("MySkeletalMesh"));
Mesh->SetSkeletalMesh(Finder_SkeletalMesh.Object);
SkeletalMeshHandle = Finder_SkeletalMesh.Object;

or override Mesh->SetSkeletalMesh().

Now you can find Socket

USkeletalMeshSocket * EnemyhandR = SkeletalMeshHandle->FindSocket("hand");

[3] Instead of moving the Socket, you can move the object that is attached to it.

Thank you, Shaggy41!
Is there no way to make it simpler? So it is impossible to move socket. Maybe I can rotate a gun attached to the socket. But would be great to use the socket near the gun barrel to make it pointing at the player. It is an idea. I need to think about that.

This is the code I got:

USkeletalMeshSocket const* MHSpine3Const = MH->GetMesh()->GetSocketByName("GunSocket");
		USkeletalMeshSocket const* EnemySpine3Const = Enemy1->GetMesh()->GetSocketByName("GunSocket");
		if(MHSpine3Const && EnemySpine3Const){
			
			USkeletalMeshSocket* MHSpine3 = const_cast<USkeletalMeshSocket*>(MHSpine3Const);
			USkeletalMeshSocket* EnemySpine3 = const_cast<USkeletalMeshSocket*>(EnemySpine3Const);
			
			if (MHSpine3 && EnemySpine3) {
				FVector MHSLoc = MHSpine3->GetSocketLocation(MH->GetMesh());
				FVector Enemy1SLoc = EnemySpine3->GetSocketLocation(Enemy1->GetMesh());
				FVector Forward = (MHSLoc - Enemy1SLoc);
				FRotator PlayerRot = FRotationMatrix::MakeFromX(Forward).Rotator();

				EnemySpine3->RelativeRotation = PlayerRot;
			}
		}

It works with sockets (sockets rotate), but now I know that I need to do the same not with sockets (it makes no sense), but with bones like spine_03 in the armature. How to get a specific bone in the armature? I need to rotate the bone of the enemy’s armature (AI bot) to follow my player character.

Why cannot I get a specific bone by:

USkeletalMeshSocket const* EnemySpine3Const = Enemy1->GetMesh()->GetSocketByName("spine_03");

Is that for sockets only?

Nobody knows?