You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

UE4.17.2第一人称模板C++实现Projectile触物爆炸功能咨询

How to Make Your UE4.17.2 C++ Projectile Explode on Contact

Hey there! Let's get that projectile exploding on contact sorted out for your first-person C++ project. I'll break this down into straightforward steps using the Starter Content explosion particle:

Step 1: Update the Projectile Header File

First, open your Projectile.h and add the necessary variables and function declarations. We need a reference to the explosion particle system, plus a function to handle collision events.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Projectile.generated.h"

// Forward declarations to avoid unnecessary includes
class UParticleSystem;
class USphereComponent;
class UProjectileMovementComponent;

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

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

    // Collision component for detecting hits
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
    USphereComponent* SphereCollision;

    // Projectile movement component
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
    UProjectileMovementComponent* ProjectileMovement;

    // Explosion particle system (assign this in the editor)
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Effects")
    UParticleSystem* ExplosionParticle;

    // Function to handle when the projectile hits something
    UFUNCTION()
    void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);

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

Step 2: Implement Logic in the Projectile Source File

Now open Projectile.cpp to set up collision binding, configure movement, and write the explosion logic. Don't forget to include the required headers!

#include "Projectile.h"
#include "Components/SphereComponent.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include "Kismet/GameplayStatics.h"

// Sets default values
AProjectile::AProjectile()
{
    // 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;

    // Create and set up the sphere collision component
    SphereCollision = CreateDefaultSubobject<USphereComponent>(TEXT("SphereCollision"));
    RootComponent = SphereCollision;
    
    // Set collision profile (make sure this profile allows hit events)
    SphereCollision->SetCollisionProfileName(TEXT("Projectile"));
    // Enable hit event generation
    SphereCollision->bGenerateHitEvents = true;

    // Bind the hit event to our OnHit function
    SphereCollision->OnComponentHit.AddDynamic(this, &AProjectile::OnHit);

    // Create and set up the projectile movement component
    ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileMovement"));
    ProjectileMovement->InitialSpeed = 3000.f;
    ProjectileMovement->MaxSpeed = 3000.f;
    ProjectileMovement->bRotationFollowsVelocity = true;
    ProjectileMovement->bShouldBounce = false;
}

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

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

// Handle collision and trigger explosion
void AProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
    // Check if we have a valid explosion particle system
    if (ExplosionParticle)
    {
        // Spawn the explosion particle at the projectile's location and rotation
        UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ExplosionParticle, GetActorLocation(), GetActorRotation());
    }

    // Destroy the projectile immediately after explosion
    Destroy();
}

Step 3: Set Up the Explosion Particle in the Editor

  1. Make sure you've imported Starter Content into your project (if not, you can add it via File > New Content > Add Starter Content).
  2. Compile your C++ code and open the Unreal Editor.
  3. Find your Projectile blueprint (or create one if you haven't already) based on your C++ class.
  4. In the Details panel, locate the Effects category and assign the P_Explosion particle system (found in StarterContent/Particles/P_Explosion) to the Explosion Particle variable.

Key Notes to Avoid Issues

  • Collision Profile: Ensure the "Projectile" collision profile is set to collide with the objects you want (e.g., WorldStatic, Pawns). You can adjust this in Edit > Project Settings > Collision > Collision Profiles.
  • Hit Event Generation: We enabled bGenerateHitEvents = true in code, but double-check in the editor that your sphere component has this enabled if you make changes there.
  • Particle Spawning: If the explosion doesn't show up, verify that ExplosionParticle is correctly assigned in the blueprint and that the particle system is enabled.

内容的提问来源于stack exchange,提问作者Ryan Perry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:07:22