APawn類,新增軸對映,使能夠控制APawn移動

01峰發表於2020-11-04

APawn類,新增軸對映,使能夠控制APawn移動

MyPawn.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Components/StaticMeshComponent.h"
#include "Camera/CameraComponent.h"
#include "MyPawn.generated.h"

UCLASS()
class FIRSTDEMO_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;

	UPROPERTY(VisibleAnywhere)
	UStaticMeshComponent* MyStaticMesh;

	UPROPERTY(VisibleAnywhere)
	UCameraComponent* MyCamera;

	UPROPERTY(EditAnywhere)
	float MaxSpeed;//新增pawn移動速度引數

private:
	//新增軸對映函式,MoveForward,MoveRight,Velocity
	//設為私有變數,Pawn類裡自身進行處理,無需暴露給外部
	void MoveForward(float Value);
	void MoveRight(float Value);
	FVector Velocity;

};

MyPawn.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "MyPawn.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;

	RootComponent=CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));

	MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
	MyStaticMesh->SetupAttachment(GetRootComponent());

	MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));
	MyCamera->SetupAttachment(GetRootComponent());

	MyCamera->SetRelativeLocation(FVector(-300.f, 0.f, 300.f));
	MyCamera->SetRelativeRotation(FRotator(-45.f, 0.f, 0.f));

	AutoPossessPlayer = EAutoReceiveInput::Player0;
	
	//初始化
	MaxSpeed = 100.f;
	Velocity = FVector::ZeroVector;

}

// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void AMyPawn::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	//在tick中新增偏移函式
	AddActorLocalOffset(Velocity * DeltaTime, true);

}

// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
	
	//繫結玩家輸入軸對映,軸對映和函式名要相對應
	PlayerInputComponent->BindAxis(TEXT("MoveForward"), this, &AMyPawn::MoveForward);
	PlayerInputComponent->BindAxis(TEXT("MoveRight"), this, &AMyPawn::MoveRight);

}

void AMyPawn::MoveForward(float Value)
{
	//向前的速度,利用Clamp 將value限制在-1,1之間,放置Ue4編輯器中數值過大,產生bug
	Velocity.X = FMath::Clamp(Value, -1.f, 1.f) * MaxSpeed;
}

void AMyPawn::MoveRight(float Value)
{
	//向右的速度,利用Clamp 將value限制在-1,1之間,放置Ue4編輯器中數值過大,產生bug
	Velocity.Y = FMath::Clamp(Value, -1.f, 1.f) * MaxSpeed;
}


相關文章