UE4 在FPS中播放粒子效果

  1. 使用UE4新建一个自带的第一人称设计游戏模板,使用C++工程并且包含初学者内容包
    在这里插入图片描述
    在这里插入图片描述
  2. 在项目中添加粒子效果,使用的函数
/**
	UWorld* :指向世界或者关卡的指针
	UParticleSystem*:指向粒子组件的指针
	const FTransform&SpawnTransform:位置引用
	UGameplayStatics::SpawnEmitterAtLocation()

*/
  1. 在子弹类中添加粒子系统组件
public:
	//子弹爆炸的粒子效果
	UPROPERTY(EditDefaultsOnly, Category = "AAA")
	class UParticleSystem* projectileParticle;

在子弹碰撞函数中添加播放碰撞的函数

	UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), projectileParticle, GetTransform());
  1. 完整的代码如下:
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

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

class USphereComponent;
class UProjectileMovementComponent;

UCLASS(config=Game)
class ATrueFPSProjectProjectile : public AActor
{
    
    
	GENERATED_BODY()

	/** Sphere collision component */
	UPROPERTY(VisibleDefaultsOnly, Category=Projectile)
	USphereComponent* CollisionComp;

	/** Projectile movement component */
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Movement, meta = (AllowPrivateAccess = "true"))
	UProjectileMovementComponent* ProjectileMovement;

public:
	//子弹爆炸的粒子效果
	UPROPERTY(EditDefaultsOnly, Category = "AAA")
	class UParticleSystem* projectileParticle;
public:
	ATrueFPSProjectProjectile();

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

	/** Returns CollisionComp subobject **/
	USphereComponent* GetCollisionComp() const {
    
     return CollisionComp; }
	/** Returns ProjectileMovement subobject **/
	UProjectileMovementComponent* GetProjectileMovement() const {
    
     return ProjectileMovement; }
};


// Copyright Epic Games, Inc. All Rights Reserved.

#include "TrueFPSProjectProjectile.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include "Components/SphereComponent.h"
#include "Kismet/GameplayStatics.h"
#include "Particles/ParticleSystem.h"

ATrueFPSProjectProjectile::ATrueFPSProjectProjectile() 
{
    
    
	// Use a sphere as a simple collision representation
	CollisionComp = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComp"));
	CollisionComp->InitSphereRadius(5.0f);
	CollisionComp->BodyInstance.SetCollisionProfileName("Projectile");
	CollisionComp->OnComponentHit.AddDynamic(this, &ATrueFPSProjectProjectile::OnHit);		// set up a notification for when this component hits something blocking

	// Players can't walk on it
	CollisionComp->SetWalkableSlopeOverride(FWalkableSlopeOverride(WalkableSlope_Unwalkable, 0.f));
	CollisionComp->CanCharacterStepUpOn = ECB_No;

	// Set as root component
	RootComponent = CollisionComp;

	// Use a ProjectileMovementComponent to govern this projectile's movement
	ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileComp"));
	ProjectileMovement->UpdatedComponent = CollisionComp;
	ProjectileMovement->InitialSpeed = 3000.f;
	ProjectileMovement->MaxSpeed = 3000.f;
	ProjectileMovement->bRotationFollowsVelocity = true;
	ProjectileMovement->bShouldBounce = true;

	// Die after 3 seconds by default
	InitialLifeSpan = 3.0f;

	
}

void ATrueFPSProjectProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
    
    
	// Only add impulse and destroy projectile if we hit a physics
	if ((OtherActor != nullptr) && (OtherActor != this) && (OtherComp != nullptr) && OtherComp->IsSimulatingPhysics())
	{
    
    
		OtherComp->AddImpulseAtLocation(GetVelocity() * 100.0f, GetActorLocation());

		Destroy();
	}

	UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), projectileParticle, GetTransform());
}
  1. 给子弹选中粒子的粒子效果
    在这里插入图片描述

  2. 点击运行效果如下:

在这里插入图片描述
aaa

猜你喜欢

转载自blog.csdn.net/wb175208/article/details/128013954