UE4 C++ Character的移动和摇头
目前知道两种方法:
看注释
.h:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "MyinPlayer.generated.h"
UCLASS()
class GETSTARTED_API AMyinPlayer : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
AMyinPlayer();
UPROPERTY(VisibleAnywhere,BlueprintReadOnly)
class USpringArmComponent* SpringArm;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
class UCameraComponent* FollowCamera;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
void MoveForward(float value);
void MoveRight(float value);
};
.cpp:
// Fill out your copyright notice in the Description page of Project Settings.
#include "Characters/Player/MyinPlayer.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/InputComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
// Sets default values
AMyinPlayer::AMyinPlayer()
{
// 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;
SpringArm=CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
SpringArm->SetupAttachment(GetRootComponent());
SpringArm->TargetArmLength = 600.0f;
SpringArm->bUsePawnControlRotation = true;
FollowCamera=CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(SpringArm, USpringArmComponent::SocketName);
FollowCamera->bUsePawnControlRotation = false;
GetCapsuleComponent()->SetCapsuleSize(25.0f, 100.0f);//设置胶囊体组件的高和宽
bUseControllerRotationYaw = false;
GetCharacterMovement()->bOrientRotationToMovement = true;//继承角色移动类里面的“将旋转朝向远动”设置为
GetCharacterMovement()->RotationRate=FRotator(0.0f, 500.0f, 0.0f);//继上面“旋转速率”
}
// Called when the game starts or when spawned
void AMyinPlayer::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyinPlayer::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AMyinPlayer::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
check(PlayerInputComponent);//是否有效,可用性的检查
PlayerInputComponent->BindAxis("MoveForward", this, &AMyinPlayer::MoveForward);//第一个是轴映射的名称
PlayerInputComponent->BindAxis("MoveRight", this, &AMyinPlayer::MoveRight);
PlayerInputComponent->BindAxis("Turn", this, &AMyinPlayer::AddControllerYawInput);
PlayerInputComponent->BindAxis("LookUp", this, &AMyinPlayer::AddControllerPitchInput);
}
void AMyinPlayer::MoveForward(float value)
{
//if ((Controller != nullptr) && (value != 0.0f)) {
//FRotator Rotation = Controller->GetControlRotation();
//FRotator YawRotation(0.0f, Rotation.Yaw, 0.0f);
//FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
//AddMovementInput(Direction, value);
//}
AddMovementInput(FollowCamera->GetForwardVector(), value);//添加移动输入
}
void AMyinPlayer::MoveRight(float value)
{
//if ((Controller != nullptr) && (value != 0.0f)) {
//FRotator Rotation = Controller->GetControlRotation();
//FRotator YawRotation(0.0f, Rotation.Yaw, 0.0f);
//FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
//AddMovementInput(Direction, value);
//}
AddMovementInput(FollowCamera->GetRightVector(), value);
}
C++设置的硬设置: