我做了一个简单的典当类,应该是一个球,四处走动并收集硬币。对于硬币收集系统,我进行了on over事件来在碰到球时通知球。这是基本代码(APlayerBall是派生到游戏中的默认典当类):

PlayerBall.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;
};

PlayerBall.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"));
}

如您所见,我添加了日志消息以查看到底是什么。经过一点调试后,我发现它由于某种原因被自己击中。游戏开始后,登录屏幕将如下所示:c&#43;&#43; - 使用On Begin Overlap事件虚幻引擎自己的Pawn类虚幻引擎C&#43;&#43;-LMLPHP为什么会发生这种情况?我怎样才能解决这个问题?

最佳答案

看起来网格组件与胶囊组件重叠。将组件添加到您的日志中进行确认。
如果您只想知道角色何时与其他角色重叠,则可以改用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++ - 使用On Begin Overlap事件虚幻引擎自己的Pawn类虚幻引擎C++,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62290837/

10-11 18:54