Hello, I’m trying to pick up a weapon and to attach it to my character using OnComponentBeginOverlap and AttachToComponent, but it seems that it is not working, and I’m not getting any errors, though. Here is the .cpp file :
// Fill out your copyright notice in the Description page of Project Settings.
#include "gmpl.h"
#include "MainCharacter.h"
#include "Engine.h"
#include "Gun.h"
// Sets default values
AGun::AGun()
{
// 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;
WeaponRoot = CreateDefaultSubobject<USceneComponent>(TEXT("Weapon Root"));
RootComponent = WeaponRoot;
WeaponMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Weapon Mesh"));
WeaponMesh->AttachToComponent(WeaponRoot, FAttachmentTransformRules::SnapToTargetNotIncludingScale);
WeaponBox = CreateDefaultSubobject<UBoxComponent>(TEXT("Weapon Sphere"));
WeaponBox->SetWorldScale3D(FVector(1.0f, 1.0f, 1.0f));
WeaponBox->bGenerateOverlapEvents = true;
WeaponBox->OnComponentBeginOverlap.AddDynamic(this, &AGun::OnPlayerEnterBox);
WeaponBox->AttachToComponent(WeaponRoot, FAttachmentTransformRules::SnapToTargetNotIncludingScale);
}
// Called when the game starts or when spawned
void AGun::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AGun::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
void AGun::OnPlayerEnterBox(UPrimitiveComponent * OverlappedComp, AActor * OtherActor, UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Purple, TEXT("OVERLAP"));
AMainCharacter *Character = Cast<AMainCharacter>(this->GetOwner());
PlayerMesh = Character->GetMesh();
PlayerMesh->GetSocketLocation(TEXT("RightHand"));
if (Character != nullptr)
{
if (Character->GetMesh() != nullptr)
{
WeaponMesh->AttachToComponent(Character->GetMesh(), FAttachmentTransformRules::SnapToTargetIncludingScale, "RightHand_Socket");
}
}
}
Here is the .h file :
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/Actor.h"
#include "Gun.generated.h"
UCLASS()
class GMPL_API AGun : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AGun();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Mesh")
class USkeletalMeshComponent *WeaponMesh;
UPROPERTY(EditAnywhere)
USceneComponent *WeaponRoot;
UPROPERTY(EditAnywhere)
UShapeComponent *WeaponBox;
UPROPERTY()
USkeletalMeshComponent *PlayerMesh;
UFUNCTION()
void OnPlayerEnterBox(UPrimitiveComponent *OverlapComp, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult);
};
So, if it was working, a debug message should appear, but it doesn’t. Could someone enlighten me on what’s wrong ?