一、 静态网格体(StaticMesh)**
1. 功能: 创建可视化的模型;
1. 默认的头文件里会有,所以不用特殊包含头文件;
2. 如果只是使用静态网格,而不编辑,不需要在.h中声明,如果需要追踪变量,
则需要:
例如:
2.1 在.h中,
UPROPERTY(EditAnywhere,Category="Mesh")
UStaticMeshComponent* MyMesh;
2.2 在.cpp文件中,设置可见网格,当然,可以设置硬编码,也可以在UE4中设置
MyMesh=CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyMesh"));
// 通常设定好组件之后,都会设定它的层级关系,大小,位置等
RootComponent=MyMesh;
or
MyMesh->SetupAttachment(RootComponent);
VisibleSphere->SetWorldScale3D(FVector(0.8f));
VisibleSphere->SetRelativeLocation(FVector(0, 0, -40.0f));
Waiting for adding ...
二、碰撞体(目前认为是)**
1.功能: 用于创造碰撞球体
USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>("TEXT(RootComponent)");
RootComponent = SphereComponent;
SphereComponent->InitSphereRadius(40.0f);
SphereComponent->SetCollisionProfileName(TEXT("Pawn"));
三、弹簧臂(SpringArm)**
/*
1. 弹簧臂是一根有弹性的线,连着摄像机和物体,当物体移动的时候,摄像头不会立即随动,
2. 而是过几秒跟上,这样看起来不会那么生硬。
*/
// 用弹簧臂使摄像机的运动变得更加的平滑自然--通常与摄像机配合使用
#include "GameFramework/SpringArmComponent.h"
...
USpringArmComponent* SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraAttachArm"));
SpringArm->SetupAttachment(RootComponent);
/*
pitch是围绕X轴旋转,也叫做俯仰角。 yaw是围绕Y轴旋转,也叫偏航角。
roll是围绕Z轴旋转,也叫翻滚角
*/
// 这里就是类似低头45°
SpringArm->SetRelativeRotation(FRotator(-45.f, 0.0, 0.0));
SpringArm->TargetArmLength = 400.0f;
SpringArm->bEnableCameraLag = true;
SpringArm->CameraLagSpeed = 3.0f;
四、摄像机(Camera)**
1. 功能: 创造一个摄像机
1. 弹簧臂组件具有一个特殊的内置插槽,我们可以将其连接到该插槽,
而不是直接连接到该组件的基座。
#include "Camera/CameraComponent.h"
...
// Create a camera and attach to our spring arm
UCameraComponent* Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("ActualCamera"));
Camera->SetupAttachment(SpringArm, USpringArmComponent::SocketName);
五、粒子系统(ParticleSystem)**
1. 功能: 添加粒子系统;
// Create a particle system that we can activate or deactivate
OurParticleSystem = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("MovementParticles"));
OurParticleSystem->SetupAttachment(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);
}
六、设置默认的玩家控制**
// 把玩家0 设置为自动(默认)控制者:--也可以在UE4中的细节面板完成
// take control of default player:
AutoPossessPlayer = EAutoReceiveInput::Player0;
1. EAutoReceiveInput 是一个枚举类型,pawn 的个数;
3. 玩家输入控制:
3.1 .h中的绑定函数
3.1.1 轴映射--连续输入
void MoveForward(float AxisValue);
void MoveRight(float AxisValue);
3.1.2 操作映射--一下一下按
void RotateSomething();
3.2 .h中位置向量
FVector CurrentVelocity;
3.3 在.cpp中
3.3.1 设置移动函数
void ACollidingPwan::MoveForward(float AxisValue)
{
// 以100单位/s 向前或者向后运动
//Clamp函数,把AxisValue的值限定在-1 到 1 ,防止多个按键控制同一个函数的累加
// 例如,Clamp(a,1.0f,2.0f); 把 a 限制在1~2;注意 .0f一定要加,不然报错
currentVelocity.X = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 100;
}
void ACollidingPwan::MoveRight(float AxisValue)
{
currentVelocity.Y = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 100;
}
void ACollidingPwan::RotateSomething()
{
}
3.3.2 绑定操作
// Called to bind functionality to input
void ACollidingPwan::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
// 第一个参数与你在UE4中的名称必须对应,然后执行相应的函数
PlayerInputComponent->BindAxis("MoveX", this, &ACollidingPwan::MoveForward);
PlayerInputComponent->BindAxis("MoveY", this, &ACollidingPwan::MoveRight);
PlayerInputComponent->BindAction("SpaceBar", IE_Pressed, this, &ACollidingPwan::RotateSomething);
3.3.3 在Tick中 刷新Pawn 的位置--相当于一个大循环,每秒执行60此Tick()
if (!currentVelocity.IsZero())
{
FVector newLocation = GetActorLocation() + (currentVelocity*DeltaTime);
SetActorLocation(newLocation);
}
七 蓝图加减乘除运算
(1)整数搜索整数,浮点搜索浮点,不可搜索加法或者减法;
八 修改角色的移动速度
1.0 把左上角的CharacterMovement 托到蓝图中;
点击角色,在细节面板中,搜索max walk,找到set max walk ,可以修改最大速度值;
2.0 如果不在BP_Character中,可以把characterMovement 变为共有(变量旁边的那个眼睛),就可以找到了
九 如何让自动生成一个角色,然后使用AI控制
小技巧是:延迟1s。目前不知道为什么。