虚幻4 ue4 学习笔记pwan篇 1.3 BindAction控制粒子组件 火焰特效

头文件部分

 1 // Fill out your copyright notice in the Description page of Project Settings.
 2 
 3 #pragma once
 4 
 5 #include "CoreMinimal.h"
 6 #include "GameFramework/Pawn.h"
 7 #include "MyPawn.generated.h"
 8 
 9 UCLASS()
10 class TEST4_API AMyPawn : public APawn
11 {
12     GENERATED_BODY()
13 
14 public:
15     // Sets default values for this pawn's properties
16     AMyPawn();
17 
18 protected:
19     // Called when the game starts or when spawned
20     virtual void BeginPlay() override;
21 
22 public:    
23     // Called every frame
24     virtual void Tick(float DeltaTime) override;
25 
26     // Called to bind functionality to input
27     virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
28     void fire();//就这一行代码
29     UParticleSystemComponent* OurParticleSystem;
30 };

cpp部分

 1 // Fill out your copyright notice in the Description page of Project Settings.
 2 
 3 #include "MyPawn.h"
 4 #include "Runtime/CoreUObject/Public/UObject/ConstructorHelpers.h" // ConstructorHelpers 引用
 5 #include "Runtime/Engine/Classes/Particles/ParticleSystemComponent.h"//UParticleSystemComponent 引用
 6 #include "Runtime/Engine/Classes/GameFramework/SpringArmComponent.h"//UStaticMeshComponent 引用
 7 // Sets default values
 8 AMyPawn::AMyPawn()
 9 {
10      // Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
11     PrimaryActorTick.bCanEverTick = true;
12     AutoPossessPlayer = EAutoReceiveInput::Player0;//这行代码不加 无法实现用户操作 此时用户视角就是当前pawn视角
13     // 创建一个可启用或停用的粒子系统
14     OurParticleSystem = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("MovementParticles"));
15     OurParticleSystem->SetupAttachment(RootComponent);//直接将粒子系统添加到根组件上
16     OurParticleSystem->bAutoActivate = true;//在场景中激活
17 
18     OurParticleSystem->SetRelativeLocation(FVector(-20.0f, 0.0f, 20.0f));
19     OurParticleSystem->SetWorldScale3D(FVector(3.0f));//SetWorldScale3D 设置组件在世界空间中的变换倍数。 倍数以原来模型大小为基准
20     static ConstructorHelpers::FObjectFinder<UParticleSystem> ParticleAsset(TEXT("/Game/StarterContent/Particles/P_Fire.P_Fire"));
21     if (ParticleAsset.Succeeded())
22     {
23         OurParticleSystem->SetTemplate(ParticleAsset.Object);
24     }
25 }
26 
27 // Called when the game starts or when spawned
28 void AMyPawn::BeginPlay()
29 {
30     Super::BeginPlay();
31 
32     
33 }
34 
35 // Called every frame
36 void AMyPawn::Tick(float DeltaTime)
37 {
38     Super::Tick(DeltaTime);
39 
40 }
41 
42 // Called to bind functionality to input
43 void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
44 {
45     Super::SetupPlayerInputComponent(PlayerInputComponent);
46     PlayerInputComponent->BindAction("fire", IE_Pressed, this, &AMyPawn::fire);
47 
48 }
49 
50 void AMyPawn::fire() {
51     OurParticleSystem->ToggleActive();//官方提供的切换状态 方法 我不建议使用 后面博客会告诉你为什么
    //建议通过ourParticleSystem->SetActive(计数器)实现;//通过计数器 切换状态
52 if (GEngine) 53 { 54 // TEXT只能接受字符串长 55 //FString fireAutoStutas = FString::FromInt(OurParticleSystem->bAutoActivate); 56 //FString fireStutas = FString::FromInt(OurParticleSystem->bIsActive); 57 GEngine->AddOnScreenDebugMessage(-1, 0.2, FColor::Red, TEXT("火焰!")); 58 59 } 60 61 //切换状态 62 63 }

 

没有摄像头组件 所以视角默认就是pwan本身 看起来很难受

posted on 2017-12-09 21:36  王子殿下  阅读(993)  评论(0编辑  收藏  举报

导航