記錄本文只是想說明,Blueprint和代碼實現保持一致,幾乎看着代碼,就能完成Blueprint裏的參數設定。 另外,指出說明Unreal Editor工程配置的基本操作。
打開Unreal Editor --> New Project,我們可以看到Unreal給我們提供了ThirdPersonGame的基礎模板,Code版本和Blueprint版本。
下面,我們嘗試看着CodeThirdPerson,由一個EmptyProject(當然,需要Include starter content),自己動手從零開始搭建起BlueprintThirdPerson。
事實上,官方已經有了這個教程,分爲22個視頻講解,挺值得一看並跟着做一遍的: https://www.youtube.com/watch?v=WiHuJcnOuVs
Blueprint裏如何設置MyCharacter的相關參數,只需要看ThirdPersonCodeGame裏Character的Declaration和構造函數。
UCLASS(config=Game)
class AThirdPersonCodeGameCharacter : public ACharacter
{
GENERATED_UCLASS_BODY()
/** Camera boom positioning the camera behind the character */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera)
TSubobjectPtr<class USpringArmComponent> CameraBoom;
/** Follow camera */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera)
TSubobjectPtr<class UCameraComponent> FollowCamera;
/** Base turn rate, in deg/sec. Other scaling may affect final turn rate. */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera)
float BaseTurnRate;
/** Base look up/down rate, in deg/sec. Other scaling may affect final rate. */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera)
float BaseLookUpRate;
AThirdPersonCodeGameCharacter::AThirdPersonCodeGameCharacter(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
// Set size for collision capsule
CapsuleComponent->InitCapsuleSize(42.f, 96.0f);
// set our turn rates for input
BaseTurnRate = 45.f;
BaseLookUpRate = 45.f;
// Don't rotate when the controller rotates. Let that just affect the camera.
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
// Configure character movement
CharacterMovement->bOrientRotationToMovement = true; // Character moves in the direction of input...
CharacterMovement->RotationRate = FRotator(0.0f, 540.0f, 0.0f); // ...at this rotation rate
CharacterMovement->JumpZVelocity = 600.f;
CharacterMovement->AirControl = 0.2f;
// Create a camera boom (pulls in towards the player if there is a collision)
CameraBoom = PCIP.CreateDefaultSubobject<USpringArmComponent>(this, TEXT("CameraBoom"));
CameraBoom->AttachTo(RootComponent);
CameraBoom->TargetArmLength = 300.0f; // The camera follows at this distance behind the character
CameraBoom->bUseControllerViewRotation = true; // Rotate the arm based on the controller
// Create a follow camera
FollowCamera = PCIP.CreateDefaultSubobject<UCameraComponent>(this, TEXT("FollowCamera"));
FollowCamera->AttachTo(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
FollowCamera->bUseControllerViewRotation = false; // Camera does not rotate relative to arm
// Note: The skeletal mesh and anim blueprint references on the Mesh component (inherited from Character)
// are set in the derived blueprint asset named MyCharacter (to avoid direct content references in C++)
}
我們看到,AThirdPersonCodeGameCharacter 是基於ACharacter,於是,我們創建一個基於Character的Blueprint:MyCharacter。
接下來,跟着構造函數,設置我們的MyCharacter。
<pre name="code" class="cpp">CameraBoom = PCIP.CreateDefaultSubobject<USpringArmComponent>(this, TEXT("CameraBoom"));
CameraBoom->AttachTo(RootComponent);
FollowCamera = PCIP.CreateDefaultSubobject<UCameraComponent>(this, TEXT("FollowCamera"));
FollowCamera->AttachTo(CameraBoom, USpringArmComponent::SocketName);
由此我們知道,需要在Components頁面裏,添加上SpringArm和Camera,Camera作爲SpringArm的子節點。
<img src="https://img-blog.csdn.net/20140725161955161?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDE1MzcwMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
</pre><pre name="code" class="cpp">// Don't rotate when the controller rotates. Let that just affect the camera.
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
由此,我們在Defaults頁面裏,確保這三個參數設置爲false。
(這三個參數來源於ACharacter繼承的APawn class,所以在Pawn類別裏。)
CameraBoom->bUseControllerViewRotation = true; // Rotate the arm based on the controller
FollowCamera->bUseControllerViewRotation = false; // Camera does not rotate relative to arm
設置這兩個參數,我們需要回到Components頁面。
選中前面添加的SpringArm,將參數Use Controller View Rotation勾選上。
選中前面添加的Camera,將參數Use Controller View Rotation的勾選去掉。
這幾個關鍵參數設置好之後,其他的就好說了。當然,你需要在Components頁面裏,指定Mesh,調整Mesh位置和Capsule大小。甚至可能還需要調整SpringArm和Camera的位置。
接下來,主要的工作是在Graph中實現一些MovementInput功能。 不詳述了。
要點是:先在Edit==>Project Settings==>Input裏填入相關AxisMappings和ActionMappings。 然後,在MyCharacter的Graph的EventGraph裏面,對照着CodeProject裏那些MoveForward和MoveRight等函數的實現,動手搭建這些功能啦。
接下是工程配置了。
需要自己創建一個基於GameMode的Blueprint:MyGame。 在Defaults頁面裏,將Default Pawn Class改爲上面自己製作的MyCharacter。
將這些參數配置給工程,有兩種方式:
方式1. Edit==>Project Settings==>Game/Maps&Modes/Default Modes,將Default GameMode修改你剛剛創建的MyGame。
方式2: 在World Settings裏的GameMode下將GameMode Override設置爲你的MyGame。
個人比較偏愛方式2。
事實上,UE裏工程的很多參數,都可以通過這兩種方式設定,Project Settings裏有Default值。但你可以在World Setting裏Override這些值。