官方例子上有錯誤,
那個攝像機應該是成員變量,才能在編輯器中顯示出來,
總動員上的api比較老了,不適合,它們用的attachTo(根組件),現在已經改爲SetupAttachment(根組件)了
即
_ourCamera->SetupAttachment(RootComponent);
_ourVisibleComponent->SetupAttachment(RootComponent);
其他的沒什麼好說的,輸入應該用pawn類,而不是actor,
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;起作用
先上圖,可以看到actor和camera和RootComponent的層級關係
設置輸入關係是在編輯->項目設置->輸入中,操作映射是一步一動,軸映射是連續動
上代碼
mypawn.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "MyPawn.generated.h"
class UCameraComponent;
UCLASS()
class MYSPAWN_DOC_API AMyPawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AMyPawn();
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;
public:
UPROPERTY(EditAnywhere)
USceneComponent* _ourVisibleComponent;
UPROPERTY(EditAnywhere)
UCameraComponent* _ourCamera;
public:
//輸入函數
void move_XAxis(float axisValue);
void move_YAxis(float axisValue);
void startGrowing();
void stopGrowing();
public:
FVector _currentVelocity;
bool _bGrowing;
};
mypawn.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyPawn.h"
#include "camera/cameraComponent.h"
#include "components/inputComponent.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;
//將該pawn設爲由最小編號玩家控制
AutoPossessPlayer = EAutoReceiveInput::Player0;
//創建可附加內容的虛擬根組件
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
//創建相機和可見對象
_ourCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("ourCamera"));
_ourVisibleComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ourVisibleComponent"));
//將相機和可見對象附加到根組件,偏移並旋轉相機
_ourCamera->SetupAttachment(RootComponent);
_ourCamera->SetRelativeLocation(FVector(-250.0f, 0.0f, 250.0f));
_ourCamera->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));
_ourVisibleComponent->SetupAttachment(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);
//縮放大小
float currentScale = _ourVisibleComponent->GetComponentScale().X;
if (_bGrowing)
{
currentScale += DeltaTime;
}
else
{
currentScale -= DeltaTime;
}
//確認不低於其實大小,或者增大之前的兩倍大小
currentScale = FMath::Clamp(currentScale, 1.0f, 2.0f);
_ourVisibleComponent->SetWorldScale3D(FVector(currentScale));
//位置
FVector newLocation = GetActorLocation() + _currentVelocity * DeltaTime;
SetActorLocation(newLocation);
}
// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
//按下或鬆開grow
PlayerInputComponent->BindAction("Grow", IE_Pressed, this, &AMyPawn::startGrowing);
PlayerInputComponent->BindAction("Grow", IE_Released, this, &AMyPawn::stopGrowing);
//每幀響應MoveX, MoveY
PlayerInputComponent->BindAxis("MoveX", this, &AMyPawn::move_XAxis);
PlayerInputComponent->BindAxis("MoveY", this, &AMyPawn::move_YAxis);
}
void AMyPawn::move_XAxis(float axisValue)
{
//以每秒100單位的速度前後移動
_currentVelocity.X = FMath::Clamp(axisValue, -1.0f, 1.0f) * 100.0f;
}
void AMyPawn::move_YAxis(float axisValue)
{
_currentVelocity.Y = FMath::Clamp(axisValue, -1.0f, 1.0f) * 100.0f;
}
void AMyPawn::startGrowing()
{
_bGrowing = true;
}
void AMyPawn::stopGrowing()
{
_bGrowing = false;
}