I;m trying to do something simple, attach an actor with it’s mesh to the default character skeleton with c++
There are no clear simple tutorials anywhere to follow my simple example so I decided to ask here.
How would I go about this. There are many examples, all confusing with different walks each different, so I decided to ask here.
So I created a socket on the skeletal part of the hand of the main default third person character that comes with a skeletal mesh.
I created a C++ actor class and I want to attach the actor with it’s mesh to the hand of the character.
I created a static mesh within the actor class.
Header file
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Engine/StaticMesh.h"
#include "Components/StaticMeshComponent.h"
#include "MyAc.generated.h"
UCLASS()
class MYPROJECT3_API AMyAc : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyAc();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
void Cone(AActor* Player);
private:
UStaticMesh* Mesh;
UStaticMeshComponent* MeshComp;
};
Cpp file
#include "UObject/ConstructorHelpers.h"
#include "MyAc.h"
// Sets default values
AMyAc::AMyAc()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
static ConstructorHelpers::FObjectFinder<UStaticMesh>CMesh(TEXT("StaticMesh'/Game/Meshes/StaticMesh1.StaticMesh1'"));
Mesh = CMesh.Object;
if (Mesh == nullptr)
{
UE_LOG(LogTemp, Warning, TEXT("ERROR FINDING STATIC MESH"));
}
else
{
MeshComp = CreateDefaultSubobject<UStaticMeshComponent>(FName("StaticMeshComponent"), true);
MeshComp ->SetStaticMesh(Mesh);
}
}
// Called when the game starts or when spawned
void AMyAc::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyAc::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
So I used constructor helpers to locate the mesh.
No errors so far to this point.
Now I’m suppose to have a function with the attachment part that I left out because I tried different ways and it does not work.
within the function I’m suppose to have I used something like:
Mesh->AttachToComponent(this, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), TEXT("hand"));
But this will not work because I need to refrence the Character somehow, what is it called, Character, Player ?
Attach to what ??
Do I also need to spawn the actor class ?
Do I need to set the mesh to root component.
I am attaching what, the mesh directly, the component of the mesh or the actor it’s self ?
I don’t want this in blueprint at all, the item I’m attaching is a simple actor with a simple mesh, has no coding, it’s not a weapon, just a simple mesh for practice for me to see how it works.
So when I hit play I want to see the actor with it’s mesh attached to the skeletal socket of the hand.
I will use key bindings later but for now I just want to see it attached at begin play.