C++ Programming Tutorials_1翻譯



所在原文目錄結構位置:

C++ Programming Guide

 |_____Programming Quick Start Guide

 |_____Introduction to C++ Programming in UE4

 |_____C++ Programming Tutorials

 |_____Managing Game Code

 |_____Development Setup

 |_____Gameplay Programming

 |_____Engine Architecture

 |_____Console Manager: Console Variables in C++

 |_____Command-Line Arguments

 |_____Assertions

 |_____Blueprint Function Libraries

 |_____Unreal Build System

 |_____Plugins

 |_____Coding Standard

 |_____Symbol Debugger


原文地址:Programming Tutorials


-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

C++ Programming Tutorials

C++編程教程


1.Player Input and Pawns 玩家輸入和Pawn(個人感覺Pawn是副角色,配角) 

 Over the course of this tutorial, you will learn to react to player input by extending the Pawn class.

通過這節教程,你將要學習通過對擴展Pawn 類對玩家的輸入作出反應

Steps:步驟:

(1).Customize A Pawn 設定一個Pawn

       If you are new to Unreal Engine 4, you might want to read ourProgramming Quick Start tutorial first. For this tutorial, we will assume you are familiar with creating a      project, adding C++ code to it, and compiling your code.

      如果你是新手,那麼看前面翻譯的文章Programming Quick Start Guide,在本節教程,我們假定你已經對創建一個項目,添加C++代碼和編譯你的代碼熟悉了.

      ①We will begin by creating a new, Basic Code project, with starter content, named "HowTo_PlayerInput". Then, we'll add a customizedPawn class, which we will call "MyPawn", to the project.

      我們從創建一個新的,基礎代碼項目,開始內容命名爲"HowTo_PlayerInput". 然後,我們在項目添加一個特定的Pawn類,我們叫它"MyPawn"

               !!!A Pawn is a type of Actor that is designed to be controlled by human players or AI.

               !!!Pawn是Actor的一種類型,被設計成由人類控制或人工智能

        ②The first thing we're going to do is set our MyPawn to respond to player input automatically upon the game starting. ThePawn class provides a variable we can set during initialization that handles this for us. InMyPawn.cpp, add the following code to AMyPawn::AMyPawn:

           我們要做的第一件事情就是設置MyPawn來自動響應遊戲一開始後玩家的輸入.Pawn類提供一個變量,我們能在初始化的時候設置這個變量.在MyPawn.cpp中,在AMyPawn::AMyPawn中添加如下代碼:

 

// Set this pawn to be controlled by the lowest-numbered player
//設置這個Pawn能被最底層的玩家控制
AutoPossessPlayer = EAutoReceiveInput::Player0;

       ③Next, we'll build a few basic Components. If you want to learn more about adding and managingComponents in code, as well as some common types of Components you will encounter, try reading ourComponents And Collision tutorial. In order to keep track of the Component we will create, we should add the following code toMyPawn.h, at the bottom of our class definition:

          下一步,我們要創建一些基礎的組件.如果你想學習更多關於在代碼中添加和管理組件,以及你會遇到一些常見的類型的組件,閱讀我們的Components And Collision tutorial.爲了跟蹤我們創建的組件,我們應該在MyPawn.h中,在類定義的底部添加如下代碼:

UPROPERTY(EditAnywhere)
USceneComponent* OurVisibleComponent;

          !!!This variable is tagged as a UPROPERTY so that it will be visible toUnreal Engine. This is important because it prevents the variable from being reset when the game is launched, or when the project or level is closed and reloaded.
          !!!這個變量被標記爲UPROPERTY ,因此,它將被暴露在虛幻引擎中,這是非常重要的,因爲它能防止在遊戲啓動,關卡被關閉或者重新加載的時候被重置,

      And back in MyPawn.cpp, we should add the following code toAMyPawn::AMyPawn:

       再看MyPawn.cpp,我們應該在AMyPawn::AMyPawn中添加如下代碼:

// 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->AttachTo(RootComponent);
OurCamera->SetRelativeLocation(FVector(-250.0f, 0.0f, 250.0f));
OurCamera->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));
OurVisibleComponent->AttachTo(RootComponent);

      ④We are now ready to save our changes and compile with the Build command in Visual Studio or the Compile button in theUnreal Engine editor

        現在,我們就可以準備保存和編譯我們的項目了,在vs中的build或者UE編譯按鈕都可以.

Now that we have a customizedPawn to react to our game's input, we'll need to define what that input will be. To do this, we'll configure our project'sInput Settings in the Unreal Engine editor.

既然我們已經設定了一個Pawn來響應我們遊戲的輸入,我們需要定義輸入是什麼,因此,我們要在虛幻編輯器中配置我們的項目輸入設置


MyPawn.h代碼:

// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.

#pragma once

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

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

public:
    // Sets default values
    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;
};

MyPawn.cpp代碼:

// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.

#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;

    // 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->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 );

}

// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
    Super::SetupPlayerInputComponent(InputComponent);

}
</pre><pre class="cpp" name="code">

(2).Configure Game Input 配置遊戲輸入

         !!!There are two types of input mappings: Action and Axis.

             Action Mappings are useful to think of as "yes or no" inputs, like the buttons on a mouse or joystick. They report when they are pressed, released, double-clicked, or held down for a short time. Discrete actions like jumping, shooting, or interacting with objects are good candidates for this type of mapping.

             Axis Mappings are continuous - think of them as "how much" inputs, like the stick on a joystick, or the position of a mouse cursor. They report their value every frame, even if they're not moving. Things with magnitude or direction, such as walking, looking around, and steering a vehicle are usually handled this way.

             While input mappings can be defined directly in code, the usual method is to define them in theUnreal Engine editor, so that's how we'll do it in this tutorial.

 

          !!!這兒有兩種輸入映射:Action(動作) 和 Axis(軸)

             動作映射是有用的,被認爲是"是或者否"的輸入,像按鈕在鼠標或操縱桿.它們在短時間內反饋被單擊,釋放,雙擊或者按住.不連續跳躍等操作、射擊、或與對象交互適合使用這種類型的映射。

             軸映射是連續的,認爲它們是“多少”輸入,像按在操縱桿上或鼠標光標的位置,它們每一幀都返回數據,即使它們沒有移動.有大小和方向,就像走路,環顧四周和車輛轉向通常用這種映射

             在代碼中可以定義直接輸入映射,通常的辦法是在虛幻編輯器中定義它們,本教程中,我們將這麼做


       ①In the Unreal Engine editor, under the Edit dropdown menu, we'll click the Project Settings option.

         在虛幻編輯器中, Edit (編輯)->Project Settings (項目設置)->Input(輸入)

    ②From there, we'll select the Input option from theEngine section on the left. We can then expand the Bindings category that appears on the right, and add oneAction Mapping and two Axis Mappings.

The plus sign next to the Action Mapping or Axis Mapping section headings will add a new mapping. The expander arrow on the left can be used to show or hide our mappings. To add an additional input to a mapping, click the plus sign next to that mapping. Following are the mappings and inputs we will need. Take note of the negative values for the "S" and A" inputs.

選輸入後,按相應的加號增加,如下所示:


       ③Now that our input is configured, let's set up a MyPawn in our level. The MyPawn class will appear in ourContent Browser, and is ready to be dragged into the Level Editor.

          既然我們已經配置了輸入,讓我們在關卡中設置MyPawn ,MyPawn 類將出現在我們的Content Browser(內容瀏覽器)中,準備好被拖到關卡編輯器中了.

     ④One more step is needed to set up our MyPawn. We'll need to give it aStatic Mesh so that we can see it in the game. We can do this by selecting theMyPawn we just created, selecting the component called "OurVisibleComponent (Inherited)" in theDetails Panel, and assigning an asset to it through the dropdown box in theStatic Mesh category. For this tutorial, "Shape_Cylinder" is a good asset to use.

      我們要多做一步來設置我們的MyPawn,我們要給他一個靜態的網格模型以便我們可以在遊戲中看到他,確保你已經把它拖到世界中後,在右邊 Details Panel中,設置Static Mesh ,這節教材中"Shape_Cylinder"(圓柱體)是一個不錯的選擇

       ⑤We can now save our level and return to Visual Studio to write code that will make the MyPawn we placed react to the inputs we defined.

          現在我們能保存下我們的關卡然後返回到VS中寫代碼讓MyPawn 響應我們定義的輸入

We're now ready to finish coding our MyPawn class in Visual Studio.

我們現在已經在VS,MyPawn類中完成了我們的代碼.

MyPawn.h

<strong>// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.

#pragma once

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

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

public:
    // Sets default values
    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;
};</strong>

MyPawn.cpp

// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.

#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;

    // 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->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 );

}

// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
    Super::SetupPlayerInputComponent(InputComponent);

}


(3).Program and Bind Game Actions

      ①In Visual Studio, open MyPawn.h and add the following code to the bottom of MyPawn's class definition:

         在VS中打開MyPawn.h,添加以下代碼到MyPawn的類的定義的底部

    

//Input functions
void Move_XAxis(float AxisValue);
void Move_YAxis(float AxisValue);
void StartGrowing();
void StopGrowing();

//Input variables
FVector CurrentVelocity;
bool bGrowing;

     !!!The four input functions are going to be bound to our input events. When they run, they will update the values stored in our new input variables, which MyPawn will use to determine what it should do during in the game.

     !!!這四個輸入函數將被綁定到我們的輸入事件.當它們運行的時候,他們將更新存儲在我們的新輸入變量的值,MyPawn將用於確定在遊戲中應該做什麼

  ②We'll switch over to MyPawn.cpp and program the four functions we just declared. Add the following code:

   我們切換到MyPawn.cpp ,定義我們前面聲明的函數.代碼如下:

void AMyPawn::Move_XAxis(float AxisValue)
{
    // Move at 100 units per second forward or backward
    CurrentVelocity.X = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 100.0f;
}

void AMyPawn::Move_YAxis(float AxisValue)
{
    // Move at 100 units per second right or left
    CurrentVelocity.Y = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 100.0f;
}

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

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

We use FMath::Clamp to constrain the values we get from our inputs to the range of -1 to +1. Although it's not an issue in this example, if there were multiple keys that could affect an axis in the same way, the values would be added together if a player pressed those inputs at the same time. For example, if both W and Up Arrow were mapped to MoveX with scales of 1.0, pressing both would result in an AxisValue of 2.0, which would let the player move at double speed if we didn't clamp it.
我們用FMath::Clamp 函數來限制值在-1~1.雖然在這個例子中這不是一個問題,但是如果這兒有很多按鍵同樣的也許會影響軸映射,如果一個玩家同一時間按下一些按鍵,這些值就加到一起,舉個例子,如果W和向上箭頭映射尺度爲1.0(w和向上都是前進按鍵),同時按住這兩個鍵就是2.0,如果我們不限制,玩家就是2倍的移動速度!!(人家不就相當於開了加速外掛了嗎?)

    !!!You may notice that the two "Move" functions take axis values as floats, while the "Grow" functions do not. This is because they will be mapped to MoveX and MoveY, which are Axis Mappings, and therefore will have a floating-point parameter. Action Mappings do not have this parameter.

    !!!你也許注意到了,兩個 Move函數值是float類型,而Grow不是,這是因爲他們要被映射到MoveX和MoveY,是軸映射,因此是float,動作映射沒有這個參數

   ③Now that we have our input functions defined, we'll need to bind them so that they will react to the appropriate inputs. Add the following code inside ofAMyPawn::SetupPlayerInputComponent:

    既然我們已經定義好了輸入函數,我們需要綁定他們,這樣他們會反應到適當的輸入,添加以下代碼到AMyPawn::SetupPlayerInputComponent: 內

// 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);

    ④We now have variables that are updated by the inputs we configured. All we have left to do is write code to do something with them. Let's add the following code to AMyPawn::Tick:

    我們現在有更新我們配置的輸入變量.剩下我們要做的就是爲他們寫一些代碼,在AMyPawn::Tick中添加如下代碼:

<strong>// Handle growing and shrinking based on our "Grow" action
{
    float CurrentScale = OurVisibleComponent->GetComponentScale().X;
    if (bGrowing)
    {
        // Grow to double size over the course of one second
        CurrentScale += DeltaTime;
    }
    else
    {
        // Shrink half as fast as we grow
        CurrentScale -= (DeltaTime * 0.5f);
    }
    // Make sure we never drop below our starting size, or increase past double size.
    CurrentScale = FMath::Clamp(CurrentScale, 1.0f, 2.0f);
    OurVisibleComponent->SetWorldScale3D(FVector(CurrentScale));
}

// Handle movement based on our "MoveX" and "MoveY" axes
{
    if (!CurrentVelocity.IsZero())
    {
        FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
        SetActorLocation(NewLocation);
    }
}</strong>
      ⑤After compling our code, we can return to the Unreal Editor and press Play. We should have control of our Pawn with the WASD keys, and we should be able to make it grow by holding Space Bar, and watch it shrink when we let go.

       編譯代碼後,我們回到虛幻編輯器,點Play運行,WASD控制方向,空格鍵控制增長,鬆開縮小.

完整的代碼:

MyPawn.h

// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.

#pragma once

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

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

public:
    // Sets default values
    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;

    // Input functions
    void Move_XAxis(float AxisValue);
    void Move_YAxis(float AxisValue);
    void StartGrowing();
    void StopGrowing();

    // Input variables
    FVector CurrentVelocity;
    bool bGrowing;
};


MyPawn.cpp

// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.

#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;

    // 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->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);

    // Handle growing and shrinking based on our "Grow" action
    {
        float CurrentScale = OurVisibleComponent->GetComponentScale().X;
        if (bGrowing)
        {
            // Grow to double size over the course of one second
            CurrentScale += DeltaTime;
        }
        else
        {
            // Shrink half as fast as we grow
            CurrentScale -= (DeltaTime * 0.5f);
        }
        // Make sure we never drop below our starting size, or increase past double size.
        CurrentScale = FMath::Clamp(CurrentScale, 1.0f, 2.0f);
        OurVisibleComponent->SetWorldScale3D(FVector(CurrentScale));
    }

    // Handle movement based on our "MoveX" and "MoveY" axes
    {
        if (!CurrentVelocity.IsZero())
        {
            FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
            SetActorLocation(NewLocation);
        }
    }
}

// Called to bind functionality to input
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)
{
    // Move at 100 units per second forward or backward
    CurrentVelocity.X = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 100.0f;
}

void AMyPawn::Move_YAxis(float AxisValue)
{
    // Move at 100 units per second right or left
    CurrentVelocity.Y = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 100.0f;
}

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

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


(4).On Your Own! 靠自己做!

Using what you have learned, try to do the following:

根據你學到的,嘗試着做以下的內容:

     ●Implement directional controls that increase speed after being held for a certain period of time.

        實現方向控制時,按住一段時間,速度暴漲(比如前進按鈕w,按下3秒後,速度變成原來的10倍)

     ●Create a special input sequence that instantly expands the object to full scale when the user presses an Action Mapping immediately after starting to press an Axis Mapping.

          創建一個指定的輸入序列,立即將對象縮放到指定倍數,也就是用動作映射和軸映射實現對象的增長/縮小

As for the specifics covered in this tutorial:至於本教程中介紹的細節:

     ●For more information about Input, try the Input page. 關於輸入的更多信息,點此鏈接

     ●For further tutorials, see the C++ Programming Tutorials page.  進一步的教程,點此鏈接


//以下代碼爲本人的作業,僅供參考

      --------------------------------解決第一個: 3秒後10倍速度的問題--------------------------------

   第一步:添加變量:    float m_TimeAdd; //時間累加

   第二步:AMyPawn構造函數中初始化爲0:   m_TimeAdd = 0.0f;  //時間累計

   第三步:修改AMyPawn::Tick函數:

 

void AMyPawn::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );

	// "Grow" 模塊
	{
		float CurrentScale = OurVisibleComponent->GetComponentScale().X;
		if (bGrowing)
		{
			// Grow to double size over the course of one second
			CurrentScale += DeltaTime;
		}
		else
		{
			// Shrink half as fast as we grow
			CurrentScale -= (DeltaTime * 0.5f);
		}
		// Make sure we never drop below our starting size, or increase past double size.
		CurrentScale = FMath::Clamp(CurrentScale, 1.0f, 2.0f);
		OurVisibleComponent->SetWorldScale3D(FVector(CurrentScale));
	}

	// WASD移動模塊
	{
		if (!CurrentVelocity.IsZero())
		{
			if (m_TimeAdd >= 3.0f)
			{
				//10倍的速度
				FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime) * 10;
				SetActorLocation(NewLocation);
			}
			else
			{
				//還是原來的速度
				FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
				SetActorLocation(NewLocation);
			}

			//加上時間間隔
			m_TimeAdd += DeltaTime;
		}
		else
		{
			//只要有停頓,從新開始累加時間
			m_TimeAdd = 0.0f;
		}
	}
}


      ------------------------------ 按空格鍵立馬變大問題--------------------------------


	// "Grow" 模塊
	{
		float CurrentScale;
		
		if (bGrowing)
		{
			//空格鍵按住,直接長大爲兩倍
			CurrentScale = 2.0f;
			OurVisibleComponent->SetWorldScale3D(FVector(CurrentScale));
		}
		else
		{
			//空格鍵鬆開,直接變爲原大小
			CurrentScale = 0.5f;
			OurVisibleComponent->SetWorldScale3D(FVector(CurrentScale));
		}
	}

動作映射和軸映射的額區別見上面

2.Game-Controlled Cameras 遊戲控制攝像機

3.Variables, Timers, and Events 變量,定時器和事件

4.Player-Controlled Cameras 玩家控制攝像機

5.Components And Collision 組件和碰撞
































發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章