新建一个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