78、The character class
要迁移文件夹时,右键选择migrate。可以跨项目migrate
创建一个c++类
80、character input
arrow component指示前方.设置网格和动画
81、character camera and springarm
蓝图中的这个选项,对应bUseControllerRotationPitch属性
use pawn control rotation可以在pawn不转动的情况下,只转动弹簧臂
但是这时候按w\s是沿着人物的前方、后方移动,而不是人物转向相机所在的方向移动。
注意 Controller和Capsule是不同物体,所以可能controller转动,但capsule不转动。
-----------SlashCharacter.h--------------
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "SlashCharacter.generated.h"class USpringArmComponent;
class UCameraComponent;UCLASS()
class MYPROJECT_API ASlashCharacter : public ACharacter
{GENERATED_BODY()public:// Sets default values for this character's propertiesASlashCharacter();// Called every framevirtual void Tick(float DeltaTime) override;// Called to bind functionality to inputvirtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;void MoveForward(float Value);void MoveRight(float Value);void Turn(float Value);void LookUp(float Value);
private:UPROPERTY(VisibleAnywhere)USpringArmComponent* CameraBoom;UPROPERTY(VisibleAnywhere)UCameraComponent* ViewCamera;
};
------SlashCharacter.cpp------------------
#include "Characters/SlashCharacter.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
// Sets default values
ASlashCharacter::ASlashCharacter()
{// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.PrimaryActorTick.bCanEverTick = true;bUseControllerRotationPitch = false;bUseControllerRotationYaw = false;bUseControllerRotationRoll = false;CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));CameraBoom->SetupAttachment(GetRootComponent());CameraBoom->TargetArmLength = 300.f;ViewCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("ViewCamera"));ViewCamera->SetupAttachment(CameraBoom);}// Called when the game starts or when spawned
void ASlashCharacter::BeginPlay()
{Super::BeginPlay();}// Called every frame
void ASlashCharacter::Tick(float DeltaTime)
{Super::Tick(DeltaTime);}// Called to bind functionality to input
void ASlashCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{Super::SetupPlayerInputComponent(PlayerInputComponent);PlayerInputComponent->BindAxis(FName("MoveForward"), this, &ASlashCharacter::MoveForward);PlayerInputComponent->BindAxis(FName("Turn"), this, &ASlashCharacter::Turn);PlayerInputComponent->BindAxis(FName("LookUp"), this, &ASlashCharacter::LookUp);PlayerInputComponent->BindAxis(FName("MoveRight"), this, &ASlashCharacter::MoveRight);
}void ASlashCharacter::MoveForward(float Value)
{if (Controller && (Value != 0.f)){FVector Forward = GetActorForwardVector();AddMovementInput(Forward, Value);}
}void ASlashCharacter::MoveRight(float Value)
{if (Controller && (Value != 0.f)){FVector Right = GetActorRightVector();AddMovementInput(Right, Value);}
}void ASlashCharacter::Turn(float Value)
{AddControllerYawInput(Value);
}void ASlashCharacter::LookUp(float Value)
{AddControllerPitchInput(Value);
}
83、control direction
实现角色朝向相机所看的角度移动
-------SlashCharacter.cpp----------
void ASlashCharacter::MoveForward(float Value)
{if (Controller && (Value != 0.f)){//find out which way is forwardconst FRotator ControlRotation = GetControlRotation();const FRotator YawRotation(0.f, ControlRotation.Yaw, 0.f);const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);AddMovementInput(Direction, Value);}
}void ASlashCharacter::MoveRight(float Value)
{if (Controller && (Value != 0.f)){const FRotator ControlRotation = GetControlRotation();const FRotator YawRotation(0.f, ControlRotation.Yaw, 0.f);const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);AddMovementInput(Direction, Value);}
}
movement组件的orient能够让角色面朝移动方向
rotation rate能够改变角色转向移动的快慢
c++实现
----------SlashCharacter.cpp--------
ASlashCharacter::ASlashCharacter()
{// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.PrimaryActorTick.bCanEverTick = true;bUseControllerRotationPitch = false;bUseControllerRotationYaw = false;bUseControllerRotationRoll = false;GetCharacterMovement()->bOrientRotationToMovement = true;GetCharacterMovement()->RotationRate = FRotator(0.f, 400.f, 0.f);CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));CameraBoom->SetupAttachment(GetRootComponent());CameraBoom->TargetArmLength = 300.f;ViewCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("ViewCamera"));ViewCamera->SetupAttachment(CameraBoom);}
84、hair and eyebrow
新增模块HairStrandsCore
----------MyProject.Build.cs-------
public class MyProject : ModuleRules
{public MyProject(ReadOnlyTargetRules Target) : base(Target){PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HairStrandsCore" });PrivateDependencyModuleNames.AddRange(new string[] { });// Uncomment if you are using Slate UI// PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });// Uncomment if you are using online features// PrivateDependencyModuleNames.Add("OnlineSubsystem");// To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true}
}
重新构建项目,删掉三个自动生成的文件然后再生成
----------.h---------------
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "SlashCharacter.generated.h"class USpringArmComponent;
class UCameraComponent;
class UGroomComponent;UPROPERTY(VisibleAnywhere, Category = Hair)UGroomComponent* Hair;UPROPERTY(VisibleAnywhere, Category = Hair)UGroomComponent* Eyebrows;
----------------.cpp---------Hair = CreateDefaultSubobject<UGroomComponent>(TEXT("Hair"));Hair->SetupAttachment(GetMesh());Hair->AttachmentName = FString("head");Eyebrows = CreateDefaultSubobject<UGroomComponent>(TEXT("Eyebrows"));Eyebrows->SetupAttachment(GetMesh());Eyebrows->AttachmentName = FString("head");
然后选择groom资产(我这里没有groom资产,所以没选