2018-10-11 2個(gè)虛幻4初始教學(xué)的 C++項(xiàng)目 筆記

關(guān)于虛幻4的C++開始項(xiàng)目解析:

虛幻4編程快速入門的項(xiàng)目是一個(gè)簡(jiǎn)單的ACTOR 持續(xù)上下移動(dòng) 的項(xiàng)目

FVector NewLocation = GetActorLocation(); //定義一個(gè)FVector
float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
NewLocation.Z += DeltaHeight * 20.0f;      //把高度以20的系數(shù)進(jìn)行縮放
RunningTime += DeltaTime;                       //運(yùn)行時(shí)間
SetActorLocation(NewLocation);

FVector 是虛幻4里面的三維坐標(biāo)
Fmath 虛幻4的數(shù)學(xué)函數(shù) Sin為正旋函數(shù) 所以物體會(huì)持續(xù)上下運(yùn)動(dòng)

RunningTime 提前定義的float

DeltaTime (Delta增量)
static double DeltaTime = 1 / 30.0;
Holds current delta time in seconds.
以秒計(jì)算,完成最后一幀的時(shí)間(只讀)。就是用“秒”取代“幀”,以秒來執(zhí)行。

玩家輸入和Pawns 項(xiàng)目:

這個(gè)項(xiàng)目需要?jiǎng)?chuàng)建繼承Pawn的類
首先我們要設(shè)置MyPawn,使之在游戲開始時(shí)能自動(dòng)響應(yīng)玩家輸入。 Pawn 類提供了一個(gè)變量,可供我們?cè)诔跏蓟瘯r(shí)進(jìn)行設(shè)置,從而為我們處理該內(nèi)容。

#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<USceneComponent>(TEXT("RootComponent"));
    // Create a camera and a visible object
    UCameraComponent* OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("OurCamera"));
    OurVisibleComponent = CreateDefaultSubobject<UStaticMeshComponent>(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();
    
}

// 在每一幀調(diào)用
void AMyPawn::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

    // 基于"Grow"操作來處理增長(zhǎng)和收縮
    {
        float CurrentScale = OurVisibleComponent->GetComponentScale().X;
        if (bGrowing)
        {
            //  在一秒的時(shí)間內(nèi)增長(zhǎng)到兩倍的大小
            CurrentScale += DeltaTime;
        }
        else
        {
            // 隨著增長(zhǎng)收縮到一半,實(shí)際上這個(gè)參數(shù)可以改,讓變小的速度變快
            CurrentScale -= (DeltaTime * 0.5f);
        }
        // 確認(rèn)永不低于起始大小,或增大之前的兩倍大小。
        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"坐標(biāo)軸來處理移動(dòng)
    {
        if (!CurrentVelocity.IsZero())
        {
            FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
            SetActorLocation(NewLocation);
        }
    }
}

// 調(diào)用以綁定功能到輸入
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單位的速度向前或向后移動(dòng)
    CurrentVelocity.X = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 100.0f;
}

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

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

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

這個(gè)包官方?jīng)]有包括進(jìn)去,但是實(shí)際在VS中要通過編譯必須加入這個(gè)包,要不然虛幻4引擎的編譯無法通過

#include "Camera/CameraComponent.h"

clamp這個(gè)函數(shù)是取后面2個(gè)值的最大或最小值,若x大于max取max,小于min取min。
乘以deltaTime這個(gè)參數(shù),可以把以100單位每幀轉(zhuǎn)化為以100單位每秒。
編譯出來的物品是沒有物理屬性的,只是按照坐標(biāo)的變化來移動(dòng)。

注意,新版的ue4引擎似乎不再使用AttachTo()這個(gè)函數(shù),而是改為SetupAttachment()這個(gè)函數(shù)。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容