2018-10-11 2个虚幻4初始教学的 C++项目 笔记

关于虚幻4的C++开始项目解析:

虚幻4编程快速入门的项目是一个简单的ACTOR 持续上下移动 的项目

FVector NewLocation = GetActorLocation(); //定义一个FVector
float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
NewLocation.Z += DeltaHeight * 20.0f;      //把高度以20的系数进行缩放
RunningTime += DeltaTime;                       //运行时间
SetActorLocation(NewLocation);

FVector 是虚幻4里面的三维坐标
Fmath 虚幻4的数学函数 Sin为正旋函数 所以物体会持续上下运动

RunningTime 提前定义的float

DeltaTime (Delta增量)
static double DeltaTime = 1 / 30.0;
Holds current delta time in seconds.
以秒计算,完成最后一帧的时间(只读)。就是用“秒”取代“帧”,以秒来执行。

玩家输入和Pawns 项目:

这个项目需要创建继承Pawn的类
首先我们要设置MyPawn,使之在游戏开始时能自动响应玩家输入。 Pawn 类提供了一个变量,可供我们在初始化时进行设置,从而为我们处理该内容。

#include "MyPawn.h"
#include "Camera/CameraComponent.h"

// Sets default values
AMyPawn::AMyPawn()
{
    // Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;

    // Set this pawn to be controlled by the lowest-numbered player
    AutoPossessPlayer = EAutoReceiveInput::Player0;

    // Create a dummy root component we can attach things to.
    RootComponent = CreateDefaultSubobject(TEXT("RootComponent"));
    // Create a camera and a visible object
    UCameraComponent* OurCamera = CreateDefaultSubobject(TEXT("OurCamera"));
    OurVisibleComponent = CreateDefaultSubobject(TEXT("OurVisibleComponent"));
    // Attach our camera and visible object to our root component. Offset and rotate the camera.
    OurCamera->SetupAttachment(RootComponent);
    OurCamera->SetRelativeLocation(FVector(-250.0f, 0.0f, 250.0f));
    OurCamera->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));
    OurVisibleComponent->SetupAttachment(RootComponent);

}

// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
    Super::BeginPlay();
    
}

// 在每一帧调用
void AMyPawn::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

    // 基于"Grow"操作来处理增长和收缩
    {
        float CurrentScale = OurVisibleComponent->GetComponentScale().X;
        if (bGrowing)
        {
            //  在一秒的时间内增长到两倍的大小
            CurrentScale += DeltaTime;
        }
        else
        {
            // 随着增长收缩到一半,实际上这个参数可以改,让变小的速度变快
            CurrentScale -= (DeltaTime * 0.5f);
        }
        // 确认永不低于起始大小,或增大之前的两倍大小。
        CurrentScale = FMath::Clamp(CurrentScale, 1.0f, 2.0f);

                //FVector(X) constructor initializing all components to a single float value.
        OurVisibleComponent->SetWorldScale3D(FVector(CurrentScale));
    }

    // 基于"MoveX"和 "MoveY"坐标轴来处理移动
    {
        if (!CurrentVelocity.IsZero())
        {
            FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
            SetActorLocation(NewLocation);
        }
    }
}

// 调用以绑定功能到输入
void AMyPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
    Super::SetupPlayerInputComponent(InputComponent);

    // Respond when our "Grow" key is pressed or released.
    InputComponent->BindAction("Grow", IE_Pressed, this, &AMyPawn::StartGrowing);
    InputComponent->BindAction("Grow", IE_Released, this, &AMyPawn::StopGrowing);

    // Respond every frame to the values of our two movement axes, "MoveX" and "MoveY".
    InputComponent->BindAxis("MoveX", this, &AMyPawn::Move_XAxis);
    InputComponent->BindAxis("MoveY", this, &AMyPawn::Move_YAxis);
}

void AMyPawn::Move_XAxis(float AxisValue)
{
    // 以每秒100单位的速度向前或向后移动
    CurrentVelocity.X = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 100.0f;
}

void AMyPawn::Move_YAxis(float AxisValue)
{
    // 以每秒100单位的速度向右或向左移动
    CurrentVelocity.Y = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 100.0f;
}

void AMyPawn::StartGrowing()
{
    bGrowing = true;
}

void AMyPawn::StopGrowing()
{
    bGrowing = false;
}

这个包官方没有包括进去,但是实际在VS中要通过编译必须加入这个包,要不然虚幻4引擎的编译无法通过

#include "Camera/CameraComponent.h"

clamp这个函数是取后面2个值的最大或最小值,若x大于max取max,小于min取min。
乘以deltaTime这个参数,可以把以100单位每帧转化为以100单位每秒。
编译出来的物品是没有物理属性的,只是按照坐标的变化来移动。

注意,新版的ue4引擎似乎不再使用AttachTo()这个函数,而是改为SetupAttachment()这个函数。

你可能感兴趣的:(2018-10-11 2个虚幻4初始教学的 C++项目 笔记)