gpt4 book ai didi

c++ - 使用 OnComponentHit UE4 C++ 进行碰撞检测

转载 作者:行者123 更新时间:2023-12-03 08:55:21 26 4
gpt4 key购买 nike

我遇到碰撞问题。我想做简单的射弹。我制作了一个具有根、球体和网格组件的 Actor。当这个 Actor 击中目标物体时,什么也没有发生。我尝试了很多方法,也有

void OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);


void OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);

功能,但没有任何结果..

这是我的自定义射弹的构造函数:

AProjectile_2::AProjectile_2()
{
// 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;

Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
RootComponent = Root;

mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("staticMesh"));
static ConstructorHelpers::FObjectFinder<UStaticMesh>SphereMeshAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
if (SphereMeshAsset.Succeeded())
{
mesh->SetStaticMesh(SphereMeshAsset.Object);
mesh->SetWorldScale3D(FVector(0.2f));
}
mesh->SetEnableGravity(true);
mesh->SetSimulatePhysics(true);
mesh->SetupAttachment(Root);

Sphere = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComponent"));
Sphere->InitSphereRadius(5.f);
Sphere->SetupAttachment(Root);

Sphere->SetCollisionProfileName(TEXT("Target"));

Sphere->OnComponentHit.AddDynamic(this, &AProjectile_2::OnHit);
Sphere->SetupAttachment(RootComponent);

InitialLifeSpan = 5.f;
}

这是一个检测函数

void AProjectile_2::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
if ((OtherActor != NULL) && (OtherActor != this) && (OtherComp != NULL))
{
if (OtherActor && (OtherActor != this) && OtherComp && OtherActor->GetClass()->IsChildOf(ADestroypack::StaticClass()))
{
destroypack = (ADestroypack*)OtherActor;
destroypack->decreasehp(50);
destroypack->check_hp();
destroypack->showhp();
}
}
}

这里我的网格正在受力移动

void AProjectile_2::Initvel(FVector Velocity)
{
mesh->AddForce(Velocity * 300);
}

这是我的角色功能,使用人民币按钮进行拍摄

void AHero::Attack_right()
{
FVector Location = GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorLocation();
FRotator Rotation = GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorRotation();

GetActorEyesViewPoint(Location, Rotation);
projectiles = (AProjectile_2*) GetWorld()->SpawnActor(AProjectile_2::StaticClass(), &Location, &Rotation);
FVector LaunchDir;
LaunchDir = GetActorForwardVector() * 5000.f;
projectiles->Initvel(LaunchDir);
}

如果有人能说出我做错了什么,或者我的项目中缺少什么,我将不胜感激。

好的!我已经弄清楚了! :D我犯了一个错误,将重力和物理添加到网格和球体中。我已将其从网格中删除,现在可以使用了:)

AProjectile_2::AProjectile_2()
{
// 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;

//Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
//RootComponent = Root;

Sphere = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComponent"));
Sphere->SetSimulatePhysics(true);
Sphere->SetEnableGravity(true);
Sphere->SetNotifyRigidBodyCollision(true);
Sphere->InitSphereRadius(40.f);
Sphere->BodyInstance.SetCollisionProfileName("BlockAllDynamic");
Sphere->SetCollisionProfileName(TEXT("Target"));
Sphere->OnComponentHit.AddDynamic(this, &AProjectile_2::OnHit);
RootComponent = Sphere;

mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("staticMesh"));
static ConstructorHelpers::FObjectFinder<UStaticMesh>SphereMeshAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
if (SphereMeshAsset.Succeeded())
{
mesh->SetStaticMesh(SphereMeshAsset.Object);
mesh->AddRelativeLocation(FVector(0.f, 0.f, 0.f));
mesh->SetWorldScale3D(FVector(0.2f));
}
/*mesh->SetEnableGravity(true);
mesh->SetSimulatePhysics(true);
mesh->SetGenerateOverlapEvents(true);*/
mesh->SetupAttachment(RootComponent);

InitialLifeSpan = 5.f;
}

最佳答案

您应该在 BeginPlay 函数中添加动态委托(delegate)。与球体组件设置相关的其余代码可以保留在构造函数中。原因在于,在 Unreal 中,如果您先制作了蓝图,将其作为射弹 cpp 类的父级,但随后添加了在构造函数中声明的 OnComponentHit 功能,则它将无法工作。如果蓝图是从一开始就使用 cpp 类(包括 OnComponentHit 委托(delegate))制作的,那么它只能在构造函数中工作。这是一个非常常见的问题,但常常被忽视。

如果您尚未添加 BeginPlay 函数,请确保将其添加到您的头文件中,

virtual void BeginPlay() override;

然后在您的 cpp 文件中声明该函数,并将对 AddDynamic 的调用从构造函数移至 BeginPlay

void AProjectile_2::BeginPlay()
{
Super::BeginPlay();
Sphere->OnComponentHit.AddDynamic(this, &AProjectile_2::OnHit);
}

关于c++ - 使用 OnComponentHit UE4 C++ 进行碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55890884/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com