this question might be fundamental but I’ll ask anyway…
I was currently working in C++, trying to set up a player character, when I ran into my problem.
How do I (for example) attach a camera component to a specific socket of my skeletal mesh, without referring to the mesh in C++ (making your code dependent on assests, etc. sounds like inherently bad news to me)?
NOTE: I want to setup (allmost) all of my code using C++, Blueprints as little as possible (I find them more difficult to write, understand and most importantly to recap than C++ code).
Thanks in advance!
The default way of setting up cameras appears to be to create a camera boom attached to the root component, and then create a camera attached to that boom. As this is all done through the root component the selected mesh has no effect
However attaching to a specific socket requires the socket to be added to the skeleton, and its’ position is entirely defined by the skeleton. The function is
SomeActor->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetIncludingScale, FName(TEXT("Awesome Socket")));
This is actually really cool, because it allows you to use the same attachment code for multiple different skeletons, and have them define where the socket is to be placed. Also by adding preview assets in the skeleton you can then tweak the position of sockets really quickly and easily.
Finally, I would just like to say if no socket is found, then whatever is attached just defaults to, I believe, the root component. In my experience, this just means that it gets attached to the very bottom of the character, then behaves normally, so even if the asset doesn’t have the correct socket it still works properly, and the problem is instantly visible.
I hope something in there helped you
Thanks for the quick response. I allready knew that a third-person camera should be attached using a SpringComponent. But I’m not trying to create a generic third person camera
To be exact I want to attach my CameraComponent to the “head” socket of my mesh so it behaves like real life eyes. Basically that’s what I want (this was done using the Blueprint editor only)
but using C++ (and then building a Blueprint out of my custom class) I only get this far…
I’m allready utilizing
FName SocketName = FName(TEXT("head"));
// I know that AttachTo() is depreciated, am going to change it soon
But at the time this C++ code gets compiled, there is no socket with this name thus the camera can’t be attached porperly (at least that’s my best guess).
Is there any option to work with assets (respectively their sockets or other properties) in C++ without explicitely specifying them?
Hey I had this exact problem when working with attach to a socket in C++ if you didn’t have everything setup properly the first time then it won’t work when you try to attach to a socket, if you’re doing this in the constructor then you probably want to remove that camera and add another one or at least change the variable name otherwise no matter what you do the changes will not take effect, if it is in the constructor use MyCamera->SetupAttachment(GetMesh(), SocketName);
For now add a new camera and attach to the head socket and compile.
I’m very sorry, but I didn’t quite understand the instruction you gave me…
Wow… it actually worked… o.o
When you created the camera the first time you did not setup the socket attachment in the constructor or maybe the mesh was not there and so unfortunately (probably a bug) you can’t go back and set it up then compile because it won’t work. I think its a blueprint issue you could try with a fresh BP but I’m not sure about that. I think if you go back now and try to change the socket to something else it won’t work not sure. anyway make sure you setup your socket attachment correctly the first time you add a new component, otherwise you’ll have to create another one just like you did now. Sorry for my bad English still learning
No problem, thanks a lot!
Gotta go, repost that question under the bug section now XD
Good idea You’re welcome.
You’ll need to create another camera try this and I’ll tell you why if this works for you.
Add a new camera let’s name it MyCamera2
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class UCameraComponent* MyCamera2;
Now in your constructor add this code:
MyCamera2 = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera2"));
MyCamera2 ->SetupAttachment(GetMesh(), SocketName);
Compile your code and see if MyCamera2 is attached to the head as it should be.