pawn控制行走和几何体放缩
官方例子上有错误,
那个摄像机应该是成员变量,才能在编辑器中显示出来,
总动员上的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;
}