UE4学习笔记14th:组件和碰撞

来源:互联网 发布:阿里云网站客服 编辑:程序博客网 时间:2024/06/05 07:13

创建项目,命名为HowTo_Components
这里写图片描述

创建新的C++类,以Pawn为夫类,命名为CollidingPawn
这里写图片描述

在CollidingPawn.h里添加:

         //追溯粒子系统组件UParticleSystemComponent *OurParticleSystem;

这里写图片描述

//创建一个可以对物理行为进行响应的球体根组件USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));RootComponent = SphereComponent;SphereComponent->InitSphereRadius(40.0f);SphereComponent->SetCollisionProfileName(TEXT("Pawn"));//

这里写图片描述

//创建并放置网格组件,这样我们能看到球体的位置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(0.0f, 0.0f, -40.0f));SphereVisual->SetWorldScale3D(FVector(0.8f));}

这里写图片描述

//创建粒子系统,供我们激活或反激活OurParticleSystem = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("MovementParticles"));OurParticleSystem->AttachTo(SphereVisual);OurParticleSystem->bAutoActivate = false;OurParticleSystem->SetRelativeLocation(FVector(-20.0f, 0.0f, 20.0f));static ConstructorHelpers::FObjectFinder<UParticleSystem> ParticleAsset(TEXT("/Game/StarterContent/Particles/P_Fire.P_Fire"));if (ParticleAsset.Succeeded()){OurParticleSystem->SetTemplate(ParticleAsset.Object);}

这里写图片描述

//使用弹簧臂使相机平滑、自然的运动
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;

这里写图片描述

//创建相机并附加到弹簧臂UCameraComponent* Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("ActualCamera"));Camera->AttachTo(SpringArm, USpringArmComponent::SocketName);////控制默认玩家AutoPossessPlayer = EAutoReceiveInput::Player0;//

这里写图片描述

现在我们新建的Pawn附加好了Component,并且可以进行配置以用于用户控制,我们现在返回编辑器。

原创粉丝点击