天天看點

UE4學習筆記14th:元件和碰撞

建立項目,命名為HowTo_Components

UE4學習筆記14th:元件和碰撞

建立新的C++類,以Pawn為夫類,命名為CollidingPawn

UE4學習筆記14th:元件和碰撞

在CollidingPawn.h裡添加:

//追溯粒子系統元件
UParticleSystemComponent *OurParticleSystem;
           
UE4學習筆記14th:元件和碰撞
//建立一個可以對實體行為進行響應的球體根元件
USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
RootComponent = SphereComponent;
SphereComponent->InitSphereRadius(f);
SphereComponent->SetCollisionProfileName(TEXT("Pawn"));//
           
UE4學習筆記14th:元件和碰撞
//建立并放置網格元件,這樣我們能看到球體的位置
UStaticMeshComponent* SphereVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
SphereVisual->AttachTo(RootComponent);
static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
if (SphereVisualAsset.Succeeded())
{
SphereVisual->SetStaticMesh(SphereVisualAsset.Object);
SphereVisual->SetRelativeLocation(FVector(f, f, -f));
SphereVisual->SetWorldScale3D(FVector(f));
}
           
UE4學習筆記14th:元件和碰撞
//建立粒子系統,供我們激活或反激活
OurParticleSystem = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("MovementParticles"));
OurParticleSystem->AttachTo(SphereVisual);
OurParticleSystem->bAutoActivate = false;
OurParticleSystem->SetRelativeLocation(FVector(-f, f, f));
static ConstructorHelpers::FObjectFinder<UParticleSystem> ParticleAsset(TEXT("/Game/StarterContent/Particles/P_Fire.P_Fire"));
if (ParticleAsset.Succeeded())
{
OurParticleSystem->SetTemplate(ParticleAsset.Object);
}
           
UE4學習筆記14th:元件和碰撞

//使用彈簧臂使相機平滑、自然的運動

USpringArmComponent* SpringArm = CreateDefaultSubobject(TEXT(“CameraAttachmentArm”));

SpringArm->AttachTo(RootComponent);

SpringArm->RelativeRotation = FRotator(-45.f, 0.f, 0.f);

SpringArm->TargetArmLength = 400.0f;

SpringArm->bEnableCameraLag = true;

SpringArm->CameraLagSpeed = 3.0f;

UE4學習筆記14th:元件和碰撞
//建立相機并附加到彈簧臂
UCameraComponent* Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("ActualCamera"));
Camera->AttachTo(SpringArm, USpringArmComponent::SocketName);//

//控制預設玩家
AutoPossessPlayer = EAutoReceiveInput::Player0;//
           
UE4學習筆記14th:元件和碰撞

現在我們建立的Pawn附加好了Component,并且可以進行配置以用于使用者控制,我們現在傳回編輯器。

繼續閱讀