如何在 C++ UE4 中使演员与角色发生碰撞?

how to make a collision of an actor on a character in C++ UE4?

我正在寻找在 c++ 中包含 unreal engine 的权力的项目,例如:

当玩家踩到它时,他会赢得蘑菇效果:

所以我创建了包含 beginoverlap 和 power 函数的 Actor Item: Item.h

#pragma once

#include "CoreMinimal.h"
#include "Components/CapsuleComponent.h"
#include "Components/SphereComponent.h"
#include "GameFramework/Actor.h"
#include "Components/StaticMeshComponent.h"
#include "GameFramework/PlayerController.h"
#include "Character/Projet2Character.h"
#include "Item.generated.h"

UCLASS()
class PROJET2_API AItem : public AActor
{
    GENERATED_BODY()
    
public: 
    // Sets default values for this actor's properties
    AItem();
protected:

    UPROPERTY(EditAnywhere)
    USphereComponent* Collider;

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

public:

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

    UFUNCTION()
    void OnEndOverlap(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
    
    UFUNCTION()
    void Power();

   AProjet2Character* player;

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

};

Item.cpp

#include "Actor/Item.h"


// Sets default values
AItem::AItem()
{
   // 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;
   
   Mesh = CreateDefaultSubobject<UStaticMeshComponent>(FName("Mesh"));
   Mesh->SetupAttachment(RootComponent);
   RootComponent = Mesh;
   
   Collider = CreateDefaultSubobject<USphereComponent>(FName("Collider"));
   Collider->SetupAttachment(Mesh);

}

// Called when the game starts or when spawned
void AItem::BeginPlay()
{
   Super::BeginPlay();
   Collider->OnComponentBeginOverlap.AddDynamic(this, &AItem::OnBeginOverlap);
   Collider->OnComponentEndOverlap.AddDynamic(this, &AItem::OnEndOverlap);
   
}

void AItem::OnBeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
   int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
   if(OtherActor->IsA(AProjet2Character::StaticClass()))
   {
     Power();
   }
}

void AItem::OnEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
   int32 OtherBodyIndex)
{
   if(OtherActor->IsA(AProjet2Character::StaticClass()))
   {
       Power();
   }
}

void AItem::Power()
{
player->GetMesh()->SetRelativeScale3D(FVector(1.5f,1.5f,1.5f));
}

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

当我在接触演员的那一刻启动游戏时,虚幻关闭并且效果仍然不适用于角色。 我想知道该怎么做? :)

感谢您的理解

没看懂。您是希望玩家踩到物体并增加尺寸,然后在他离开物体时,return 将其恢复到原始尺寸 (1) 还是保持尺寸 (2)?

//.h

    UFUNCTION()
    void Power();

    UFUNCTION()
    void ResetPower();

    bool bIsPower = false;
//.cpp

void AItem::ResetPower()
{
    player->GetMesh()->SetRelativeScale3D(FVector(1.f,1.f,1.f));
}

1:

//.cpp

void AItem::OnBeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
   int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
   if(OtherActor->IsA(AProjet2Character::StaticClass()) && !bIsPower)
   {
      player = Cast<AProjet2Character>(OtherActor);
        bIsPower = true;
        Power();
   }
}

void AItem::OnEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
   int32 OtherBodyIndex)
{
   if(OtherActor->IsA(AProjet2Character::StaticClass()) && bIsPower)
   {
       player = Cast<AProjet2Character>(OtherActor);
       bIsPower = false;
       ResetPower();
   }
}

2:

void AItem::OnBeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
   int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
   if(OtherActor->IsA(AProjet2Character::StaticClass()) && !bIsPower)
   {
      player = Cast<AProjet2Character>(OtherActor);
        bIsPower = true;
        Power();
   }
}

void AItem::OnEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
   int32 OtherBodyIndex)
{
   //Clear
}