Unreal Engine C++ 入门02

相机控制

1.新建 一个空白项目
项目类型 选择C++
在这里插入图片描述
增加一个pawn 类

在这里插入图片描述

外部事件输入设置
在这里插入图片描述
在这里插入图片描述

h文件
#pragma once

#include “CoreMinimal.h”
#include “GameFramework/SpringArmComponent.h”
#include “Camera/CameraComponent.h”
#include “GameFramework/Pawn.h”
#include “PawnWithCamera.generated.h”

UCLASS()
class HOWTO_PLAYERCAMERA_API APawnWithCamera : public APawn
{
GENERATED_BODY()

public:
// Sets default values for this pawn’s properties
APawnWithCamera();

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;

protected:
UPROPERTY(EditAnywhere)

USpringArmComponent* OurCameraSpringArm;
UCameraComponent* OurCamera;



//Input variables
FVector2D MovementInput;
FVector2D CameraInput;
float ZoomFactor;
bool bZoomingIn;

//Input functions
void MoveForward(float AxisValue);
void MoveRight(float AxisValue);
void PitchCamera(float AxisValue);
void YawCamera(float AxisValue);
void ZoomIn();
void ZoomOut();

};
cpp文 件

#include “PawnWithCamera.h”
#include “HowTo_PlayerCamera.h”

// Sets default values
APawnWithCamera::APawnWithCamera()
{
// 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;
//Create our components
RootComponent = CreateDefaultSubobject(TEXT(“RootComponent”));
OurCameraSpringArm = CreateDefaultSubobject(TEXT(“CameraSpringArm”));
OurCameraSpringArm->SetupAttachment(RootComponent);
OurCameraSpringArm->SetRelativeLocationAndRotation(FVector(0.0f, 0.0f, 50.0f), FRotator(-60.0f, 0.0f, 0.0f));
OurCameraSpringArm->TargetArmLength = 400.f;
OurCameraSpringArm->bEnableCameraLag = true;
OurCameraSpringArm->CameraLagSpeed = 3.0f;
OurCamera = CreateDefaultSubobject(TEXT(“GameCamera”));
OurCamera->SetupAttachment(OurCameraSpringArm, USpringArmComponent::SocketName);

//Take control of the default Player
AutoPossessPlayer = EAutoReceiveInput::Player0;

}

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

}

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

//Zoom in if ZoomIn button is down, zoom back out if it's not
{
    if (bZoomingIn)
    {
        ZoomFactor += DeltaTime / 0.5f;         //Zoom in over half a second
    }
    else
    {
        ZoomFactor -= DeltaTime / 0.25f;        //Zoom out over a quarter of a second
    }
    ZoomFactor = FMath::Clamp<float>(ZoomFactor, 0.0f, 1.0f);
    //Blend our camera's FOV and our SpringArm's length based on ZoomFactor
    OurCamera->FieldOfView = FMath::Lerp<float>(90.0f, 60.0f, ZoomFactor);
    OurCameraSpringArm->TargetArmLength = FMath::Lerp<float>(400.0f, 300.0f, ZoomFactor);
}

//Rotate our actor's yaw, which will turn our camera because we're attached to it
{
    FRotator NewRotation = GetActorRotation();
    NewRotation.Yaw += CameraInput.X;
    SetActorRotation(NewRotation);
}

//Rotate our camera's pitch, but limit it so we're always looking downward
{
    FRotator NewRotation = OurCameraSpringArm->GetComponentRotation();
    NewRotation.Pitch = FMath::Clamp(NewRotation.Pitch + CameraInput.Y, -80.0f, -15.0f);
    OurCameraSpringArm->SetWorldRotation(NewRotation);
}


 
//Handle movement based on our "MoveX" and "MoveY" axes
{
    if (!MovementInput.IsZero())
    {
        //Scale our movement input axis values by 100 units per second

       // MovementInput.GetSafeNormal
        MovementInput = MovementInput.GetSafeNormal() * 100.0f;
        FVector NewLocation = GetActorLocation();
        NewLocation += GetActorForwardVector() * MovementInput.X * DeltaTime;
        NewLocation += GetActorRightVector() * MovementInput.Y * DeltaTime;
        SetActorLocation(NewLocation);
    }
}

}

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

//Hook up events for "ZoomIn"
InputComponent->BindAction("ZoomIn", IE_Pressed, this, &APawnWithCamera::ZoomIn);
InputComponent->BindAction("ZoomIn", IE_Released, this, &APawnWithCamera::ZoomOut);

//Hook up every-frame handling for our four axes
InputComponent->BindAxis("MoveForward", this, &APawnWithCamera::MoveForward);
InputComponent->BindAxis("MoveRight", this, &APawnWithCamera::MoveRight);
InputComponent->BindAxis("CameraPitch", this, &APawnWithCamera::PitchCamera);
InputComponent->BindAxis("CameraYaw", this, &APawnWithCamera::YawCamera);

}

//Input functions
void APawnWithCamera::MoveForward(float AxisValue)
{
MovementInput.X = FMath::Clamp(AxisValue, -1.0f, 1.0f);
}

void APawnWithCamera::MoveRight(float AxisValue)
{
MovementInput.Y = FMath::Clamp(AxisValue, -1.0f, 1.0f);
}

void APawnWithCamera::PitchCamera(float AxisValue)
{
CameraInput.Y = AxisValue;
}

void APawnWithCamera::YawCamera(float AxisValue)
{
CameraInput.X = AxisValue;
}

void APawnWithCamera::ZoomIn()
{
bZoomingIn = true;
}

void APawnWithCamera::ZoomOut()
{
bZoomingIn = false;
}

// 最后把创建的类拖入关卡编辑器内

运行就能看效果了

在vs2019编译时有关闭引擎编辑器,
还有就是经常有清理一下项目
教程是学习官方文 档的,
官方文 档代码有些地方是有错误 的
比如头文 件
#include “GameFramework/SpringArmComponent.h”
#include “Camera/CameraComponent.h”

官方
MovementInput = MovementInput.SafeNormal() * 100.0f;
修之后的
MovementInput = MovementInput.GetSafeNormal() * 100.0f;

Unreal Engine 4 Scripting with C++ Cookbook 2016 | ISBN-10: 1785885545 | 431 pages | PDF | 7 MB Key Features A straightforward and easy-to-follow format A selection of the most important tasks and problems Carefully organized instructions to solve problems efficiently Clear explanations of what you did Solutions that can be applied to solve real-world problems Book Description Unreal Engine 4 (UE4) is a complete suite of game development tools made by game developers, for game developers. With more than 100 practical recipes, this book is a guide showcasing techniques to use the power of C++ scripting while developing games with UE4. It will start with adding and editing C++ classes from within the Unreal Editor. It will delve into one of Unreal's primary strengths, the ability for designers to customize programmer-developed actors and components. It will help you understand the benefits of when and how to use C++ as the scripting tool. With a blend of task-oriented recipes, this book will provide actionable information about scripting games with UE4, and manipulating the game and the development environment using C++. Towards the end of the book, you will be empowered to become a top-notch developer with Unreal Engine 4 using C++ as the scripting language. What you will learn Build function libraries (Blueprints) containing reusable code to reduce upkeep Move low-level functions from Blueprint into C++ to improve performance Abstract away complex implementation details to simplify designer workflows Incorporate existing libraries into your game to add extra functionality such as hardware integration Implement AI tasks and behaviors in Blueprints and C++ Generate data to control the appearance and content of UI elements
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值