UE4學習筆記4th:編程和綁定遊戲操作

在這一節中,我們來處理對輸入的響應

雙擊打開MyPawn在vs中進行編輯。
這裏寫圖片描述

在MypPawn.h中添加以下定義:

    void Move_XAxis(float AxisValue);
    void Move_YAxis(float AxisValue);
    void StartGrowing();
    void StopGrowing();
    //輸入變量
    FVector CurrentVelocity;
    bool bGrowing;

這裏寫圖片描述

前兩個用於前後左右方向上的處理,後兩個處理跳躍。
它們在運行時,會改變變量中的值,從而確定在遊戲中應執行的操作。

切換到MyPawn.cpp,對這四個函數進行編程。
在最下方輸入

void AMyPawn::Move_XAxis(float AxisValue)
{
    //前後
    CurrentVelocity.X = FMath::Clamp(AxisValue, -1.0f, 1.0f)*100.0f;
}

void AMyPawn::Move_YAxis(float AxisValue)
{
    //左右
    CurrentVelocity.Y = FMath::Clamp(AxisValue, -1.0f, 1.0f)*100.0f;

}

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

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

這裏寫圖片描述

現在我們已經完成了對輸入函數的定義,接下來要將它們進行綁定

在AMyPawn::SetupPlayerInputComponent函數中輸入以下代碼:

//響應Grow鍵
InputComponent->BindAction("Grow",IE_Pressed,this,&AMyPawn::StartGrowing);
InputComponent->BindAction("Grow",IE_Released,this,&AMyPawn::StopGrowing);

//響應兩個座標軸值
InputComponent->BindAxis("MoveX", this, &AMyPawn::Move_XAxis);
InputComponent->BindAxis("MoveY", this, &AMyPawn::Move_YAxis);

現在我完成了對輸入的綁定,接下來我要將它們在視口中體現出來。
在AMyPawn::Tick中添加如下代碼:

//基於Grow操作來處理增長和收縮
    {
        float CurrentScale = OurVisibleComponent->GetComponentScale().X;
        if (bGrowing)
        {
            //在一秒內增長到兩倍大小
            CurrentScale += DeltaTime;
        }
        else
        {
            //隨着增長收縮到一半
            CurrentScale -= (DeltaTime*0.5f);
        }
        //確認不小於初始大小、增長前的兩倍大小
        CurrentScale = FMath::Clamp(CurrentScale, 1.0f, 2.0f);
        OurVisibleComponent->SetWorldScale3D(FVector(CurrentScale));
    }
    //基於MoveX和MoveY來處理移動
    {
        if (!CurrentVelocity.IsZero())
        {
            FVector NewLoaction = GetActorLocation() + (CurrentVelocity*DeltaTime);
            SetActorLocation(NewLoaction);
        }
    }

完成後編譯,效果如下
這裏寫圖片描述

這裏寫圖片描述
編譯

編譯完代碼後,在編輯器中按下 Play ,應該可以使用WASD鍵來控制 Pawn ,而且我們應該可以通過按住空格鍵來使之增大,並在鬆開後看着其縮小。

下面是完整的代碼:

MyPawn.h
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "GameFramework/Pawn.h"
#include "MyPawn.generated.h"

UCLASS()
class HOWTO_PLAYERINPUT_API AMyPawn : public APawn
{
    GENERATED_BODY()

public:
    // Sets default values for this pawn's properties
    AMyPawn();

    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

    // Called every frame
    virtual void Tick( float DeltaSeconds ) override;

    // Called to bind functionality to input
    virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;

    UPROPERTY(EditAnywhere)
        USceneComponent* OurVisibleComponent;
    //輸入函數
    void Move_XAxis(float AxisValue);
    void Move_YAxis(float AxisValue);
    void StartGrowing();
    void StopGrowing();
    //輸入變量
    FVector CurrentVelocity;
    bool bGrowing;

};


MyPawn.cpp
// Fill out your copyright notice in the Description page of Project Settings.

#include "HowTo_PlayerInput.h"
#include "MyPawn.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;
    //設置爲玩家控制
    AutoPossessPlayer = EAutoReceiveInput::Player0;

    //添加一個可以添加對象的空根組件
    RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
    //創建相機和可見項目
    UCameraComponent* OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("OurCamera"));
    OurVisibleComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("OurVisibleComponent"));
    //附加相機和可見對象到根組建,偏移並旋轉相機。
    OurCamera->AttachTo(RootComponent);
    OurCamera->SetRelativeLocation(FVector(-250.0f, 0.0f, 250.0f));
    OurCamera->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));
    OurVisibleComponent->AttachTo(RootComponent);

}

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

}

// Called every frame
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);
        OurVisibleComponent->SetWorldScale3D(FVector(CurrentScale));
    }
    //基於MoveX和MoveY來處理移動
    {
        if (!CurrentVelocity.IsZero())
        {
            FVector NewLoaction = GetActorLocation() + (CurrentVelocity*DeltaTime);
            SetActorLocation(NewLoaction);
        }
    }
}

// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
    Super::SetupPlayerInputComponent(InputComponent);
    //響應Grow鍵
    InputComponent->BindAction("Grow",IE_Pressed,this,&AMyPawn::StartGrowing);
    InputComponent->BindAction("Grow",IE_Released,this,&AMyPawn::StopGrowing);

    //響應兩個座標軸值
    InputComponent->BindAxis("MoveX", this, &AMyPawn::Move_XAxis);
    InputComponent->BindAxis("MoveY", this, &AMyPawn::Move_YAxis);
}

void AMyPawn::Move_XAxis(float AxisValue)
{
    //前後
    CurrentVelocity.X = FMath::Clamp(AxisValue, -1.0f, 1.0f)*100.0f;
}

void AMyPawn::Move_YAxis(float AxisValue)
{
    //左右
    CurrentVelocity.Y = FMath::Clamp(AxisValue, -1.0f, 1.0f)*100.0f;

}

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

void AMyPawn::StopGrowing()
{
    bGrowing = false;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章