如何检测演员是否在虚幻引擎C ++中被击中?

问题描述 投票:0回答:2

我是虚幻引擎的新手。我正在尝试创建一个基本的游戏,在该游戏中您可以控制球并在周围滚动,以免掉出管子或其他东西。我的球看起来像这样:

PlayerBall.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "PlayerBall.generated.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="Camera")
    class UCameraComponent* Camera;

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

public: 
    // Called every frame
    void Tick(float DeltaTime) override;

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

};

PlayerBall.cpp

#include "PlayerBall.h"
#include "Camera/CameraComponent.h"

// Sets default values
APlayerBall::APlayerBall() {
    // Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;

    Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
    const ConstructorHelpers::FObjectFinder<UStaticMesh> SphereRef(TEXT("StaticMesh'/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere'"));
    Mesh->SetStaticMesh(SphereRef.Object);
    Mesh->SetSimulatePhysics(true);
    Mesh->SetEnableGravity(true);
    SetRootComponent(Mesh);

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

    AutoPossessPlayer = EAutoReceiveInput::Player0;
}

// Called when the game starts or when spawned
void APlayerBall::BeginPlay() {
    Super::BeginPlay();

}

// Called every frame
void APlayerBall::Tick(float DeltaTime) {
    Super::Tick(DeltaTime);

}

// Called to bind functionality to input
void APlayerBall::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) {
    Super::SetupPlayerInputComponent(PlayerInputComponent);

}

我还有其他障碍和障碍。但是,我也想要一些障碍物,如果它撞到你,就会杀死你。我想要APlayerBall类中的某种方法,该方法在命中时被调用,然后可以检查所命中的actor的类型。我能做什么?我使用什么组件?n我在文档中花了一个小时,但找不到任何东西。

我尝试添加RecieveHit方法,但是它说它不会从超类中重写。我这样添加它:

void ReceiveHit
(
class UPrimitiveComponent * MyComp,
AActor * Other,
class UPrimitiveComponent * OtherComp,
bool bSelfMoved,
FVector HitLocation,
FVector HitNormal,
FVector NormalImpulse,
const FHitResult & Hit
) override;
c++ unreal-engine4
2个回答
0
投票

这将是AActor::ReceiveHit,因为PlayerBall继承自APawn,而APawn继承自AActor。


0
投票

所以我终于得到了答案。我需要使用一个看起来像这样的函数:

void APlayerBall::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, 
    bool bFromSweep, const FHitResult& SweepResult) {

    UE_LOG(LogTemp, Warning, TEXT("HIT"));

}

您将UE_LOG(LogTemp, Warning, TEXT("HIT"));替换为所需的位置。然后在构造函数中添加以下内容:

Mesh->OnComponentBeginOverlap.AddDynamic(this, &APlayerBall::OnOverlapBegin);

其中Mesh是您拥有的UStaticMeshComponent的名称。当我运行它时,它记录为HIT。另外,请确保在头文件中将其标记为UFUNCTION,否则您的代码将无法编译。

我需要帮助的唯一部分是参数。我可以得到OtherActor是打你的演员,OverlappedComp是重叠的元素,OtherComp是打你的元素,但我不明白其他人的意思。

© www.soinside.com 2019 - 2024. All rights reserved.