UE4 C++ Pawn 移动

这篇博客详细介绍了如何在UE4中创建一个名为FirstPawn的 Pawn 类,并为其添加了静态网格组件、相机臂组件和跟随相机组件,用于角色的视觉表现和移动控制。通过设置移动速度和方向,实现了角色的前后左右移动。此外,还设置了输入绑定以响应玩家的移动指令。
摘要由CSDN通过智能技术生成

新建一个Pawn类FirstPawn,UE4在创建这个类的时候,会自动的添加添加一个字符A变成AFirstPawn.

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "FirstPawn.generated.h"

UCLASS()
class TRACELENGTH_API AFirstPawn : public APawn
{
	GENERATED_BODY()

public:
	// Sets default values for this pawn's properties
	AFirstPawn();

	/**Mesh component to give the Pawn a visual representation in the world*/
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
	class UStaticMeshComponent* MeshComponent;

	/** Camera boom positioning the camera behind the Pawn */
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=	Camera, meta = (AllowPrivateAccess = "true"))
	class USpringArmComponent* CameraBoom;

	/** Follow camera */
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=	Camera, meta = (AllowPrivateAccess = "true"))
	class UCameraComponent* FollowCamera;

	//移动速度
	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Movement")
	float moveSpeed = 50.0;

	//移动方向
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Movement")
	FVector direction = FVector(0.0);
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;

private:
	void moveForward(float value);
	void moveRight(float value);
	void moveUp(float value);
};

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


#include "FirstPawn.h"
#include "GameFramework\SpringArmComponent.h"
#include "Camera\CameraComponent.h"

// Sets default values
AFirstPawn::AFirstPawn() {
	// 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;

	MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent"));
	SetRootComponent(Cast<USceneComponent>(MeshComponent));

	CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
	CameraBoom->SetupAttachment(RootComponent);

	FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
	FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);

}

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

}

// Called every frame
void AFirstPawn::Tick(float DeltaTime) {
	Super::Tick(DeltaTime);

	FVector location = GetActorLocation();
	FVector detailLocation =  direction;
	detailLocation.Normalize();

	detailLocation *= moveSpeed;
	location += detailLocation;

	SetActorLocation(location);

}

// Called to bind functionality to input
void AFirstPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) {
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	check(PlayerInputComponent);

	PlayerInputComponent->BindAxis("MoveForward", this, &AFirstPawn::moveForward);
	PlayerInputComponent->BindAxis("MoveRight", this, &AFirstPawn::moveRight);
	PlayerInputComponent->BindAxis("MoveUp", this, &AFirstPawn::moveUp);
}

void AFirstPawn::moveForward(float value) {
	//UE_LOG(LogTemp, Warning, TEXT("moveForward:%f"), value);

	direction.X = value;

}

void AFirstPawn::moveRight(float value) {
	//UE_LOG(LogTemp, Warning, TEXT("moveRight: %f"), value);

	direction.Y = value;
}

void AFirstPawn::moveUp(float value) {
	direction.Z = value;
}


手动添加摄像机摇臂的角度:
在这里插入图片描述
点击【运行】即可
在这里插入图片描述
aaa

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wb175208

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值