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

时间:2019-04-28 13:47:13

标签: c++ unreal-engine4

我有碰撞问题。我要制作简单的弹丸。我制作了一个具有Root,Sphere和Mesh组件的Actor。当此演员击中目标对象时,什么也不会发生。 我尝试了很多方法,

edit file /etc/ssh/sshd_config
set PasswordAuthentication yes


/etc/init.d/ssh restart

功能,但没有任何结果。

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

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);
}

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

好!我知道了! :D 将“重力和物理”添加到网格和球体时,我犯了一个错误。我已将其从网格中删除,现在可以正常使用了:)

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);
}

2 个答案:

答案 0 :(得分:0)

如果您使用的是OnOverlapBegin,则您的组件应该使用Overlap来针对其他组件而不是Block进行碰撞响应。

答案 1 :(得分:0)

您应该在BeginPlay函数中添加动态委托。与Sphere组件的设置有关的其余代码可以保留在构造函数中。原因是在虚幻引擎中,如果您首先制作了蓝图,将其作为子对象cpp类的父项,然后又在构造函数中声明了OnComponentHit功能,则它将无法正常工作。如果该蓝图从一开始就由cpp类(包括OnComponentHit委托)构成,则仅在构造函数中有效。这是一个非常常见的问题,经常会被忽略。

请确保将BeginPlay函数添加到您的头文件中,

virtual void BeginPlay() override;

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

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