Allowing a character to walk into and destroy destructibles?

Hey all, I’m trying to give my player some physical presence in the world. Specifically for the purpose of allowing him to walk through destructibles (colliding with and destroying them). Any idea on how I should start going about this?

If you want to do it in blueprints, select a blueprint that you wish to make destructible and open it in the Blueprint editor. Right click on the event graph and type “Event ActorBeginOverlap”, then select Event ActorBeginOverlap to add the node to the graph. Right-click somewhere else and type in “Destroy Actor”, then click on Destroy Actor to add it to the graph. Drag from the execution pin (the triangular one pointing to the right) on Event ActorBeginOverlap to the input pin (it looks the same, but it’s on the right side) on DestroyActor.

If you want to do it in C++, create a Go to File->New C++ Class… and set the parent class to Actor. Then you can name it to DestructibleActor or something similar. Once that is done, you can open the new C++ class in your IDE (Xcode if you are on a mac, Visual Studio if you are on Windows). Inside the class declaration, add the following:

/** called when projectile hits something */
   UFUNCTION()
   void OnHit(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);

You also have to add a custom constructor declaration as well as a declaration for a collision component (in this example, a USphereComponent). This means that your C++ header will look something like this:

class YOURGAMENAME_API ADestructibleActor : public AActor
        {
            GENERATED_BODY()
        protected:
/** Sphere collision component */
    UPROPERTY(VisibleDefaultsOnly, Category=Projectile)
    USphereComponent* CollisionComp;
            /** called when projectile hits something */
           UFUNCTION()
           void OnHit(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
    
    public:
    ADestructibleActor(const FObjectInitializer& ObjectInitializer);
            
        };   

In the cpp file, write the following constructor:

AFPSProjectile::AFPSProjectile(const FObjectInitializer& ObjectInitializer)
        : Super(ObjectInitializer)
    {
        // Use a sphere as a simple collision representation
        CollisionComp = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("SphereComp"));
        CollisionComp->InitSphereRadius(15.0f);
CollisionComp->OnComponentHit.AddDynamic(this, &AFPSProjectile::OnHit);
        RootComponent = CollisionComp;
    }

Then add the following function definition to the code:

void ADestructibleActor::OnHit(AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
   {
       if ( OtherActor && (OtherActor != this) && OtherComp )
       {
           Destroy();
       }
   }

You can made additional changes in case you wish to only allow certain kinds of actors to destroy your destructible. For example, you can add code to specify that the destructible only be destroyed if the other actor is a subclass of a certain class, such as ACharacter.