gpt4 book ai didi

c++ - Pawn 类使用 On Begin 重叠事件自行命中 Unreal Engine C++

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

我做了一个简单的 pawn 类,它应该是一个球,可以四处走动并收集硬币。对于硬币收集系统,我制作了重叠事件以在球被击中时通知球。这是基本代码(APlayerBall 是生成到游戏中的默认 pawn 类):

球员球.h

UCLASS()
class ROLLINGBALL_API APlayerBall : public APawn
{
GENERATED_BODY()

public:
// Sets default values for this pawn's properties
APlayerBall();

UPROPERTY(VisibleAnywhere, Category="Mesh")
UStaticMeshComponent* Mesh;

UPROPERTY(VisibleAnywhere, Category="Mesh")
class UCameraComponent* Camera;

UPROPERTY(VisibleAnywhere, Category="Mesh")
class UCapsuleComponent* Capsule;

protected:
// Called when the game starts or when spawned
void BeginPlay() override;

UFUNCTION()
void BeginOverlap(UPrimitiveComponent* OverlappedComponent,
AActor* OtherActor,
UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex,
bool bFromSweep,
const FHitResult &SweepResult );
public:
// Called every frame
void Tick(float DeltaTime) override;

// Called to bind functionality to input
void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};

球员球.cpp

构造函数:
APlayerBall::APlayerBall() {
Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
Mesh->SetStaticMesh(ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere'")).Object);
Mesh->SetSimulatePhysics(true);
Mesh->SetEnableGravity(true);
SetRootComponent(Mesh);

Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
Camera->SetRelativeLocation(FVector(-500.0f, 0.0f, BaseEyeHeight));
Camera->SetupAttachment(RootComponent);

Capsule = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Capsule"));
Capsule->OnComponentBeginOverlap.AddDynamic(this, &APlayerBall::BeginOverlap);
Capsule->SetupAttachment(Mesh);

AutoPossessPlayer = EAutoReceiveInput::Player0;
}

BeginOverlap方法:
void APlayerBall::BeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) {
UE_LOG(LogTemp, Warning, TEXT("%s"), *OtherActor->GetHumanReadableName());
UE_LOG(LogTemp, Warning, TEXT("%s"), *GetHumanReadableName());
UE_LOG(LogTemp, Warning, TEXT("%s"), OtherActor == this ? TEXT("The Colliding Actor Is Myself") : TEXT("The Colliding Actor Is Not Myself"));
}

如您所见,我添加了日志消息以查看究竟是什么击中了它。经过一些调试,我发现它由于某种原因被自己击中了。游戏一开始,登录界面如下所示: enter image description here为什么会这样?我怎样才能解决这个问题?

最佳答案

看起来网格组件与胶囊组件重叠。将该组件添加到您的日志中以进行确认。
如果您只想知道您的 Actor 何时与另一个 Actor 重叠,您可能需要使用 AActor::NotifyActorBeginOverlap反而。
如果您想使用组件重叠,但又想忽略您自己的 actor 中其他组件的重叠,那么您必须自己在 BeginOverlap 中过滤掉它们。或调整您的物理碰撞配置文件,使每个组件具有不同的对象类型,不会为彼此生成重叠事件。如果是我,我会做一个像这样的过滤器:

void APlayerBall::BeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) {
if (this == OtherActor) {
// The Colliding Actor Is Myself. Ignoring because I don't care about this for some reason.
UE_LOG(LogTemp, Warning, TEXT("Ignoring component overlap of my own components"));
return;
}
UE_LOG(LogTemp, Warning, TEXT("%s"), *OtherActor->GetHumanReadableName());
UE_LOG(LogTemp, Warning, TEXT("%s"), *GetHumanReadableName());
}

关于c++ - Pawn 类使用 On Begin 重叠事件自行命中 Unreal Engine C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62290837/

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