记录本文只是想说明,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这些值。