C++ Programming Tutorials



所在原文目录结构位置:C++ Programming Guide|_____Introduction to C++ Programming in UE4|_____C++ Programming Tutorials|_____Managing Game Code|_____Development Setup|_____Gameplay Programming|_____Engine Architecture|_____Console Manager: Console Variables in C++|_____Command-Line Arguments|_____Assertions|_____Blueprint Function Libraries|_____Unreal Build System|_____Plugins|_____Coding Standard|_____Symbol Debugger

原文地址:Player-Controlled Cameras

——————————————————————————————————————————————————-

C++ Programming TutorialsC++编程教程C++ Programming Tutorials_1

见译文:C++ Programming Tutorials 2见译文:C++ Programming Tutorials 3This tutorial will show you how to activate a camera, and change your active camera from one to another.本教程展示如何激活一个摄像机,和在摄像机之间切换激活.1.Attach A Camera To A Pawn绑定一个摄像机到Pawn !!!If you are new to Unreal Engine 4, you might want to read ourProgramming Quick Start tutorial first. For this tutorial, we will assume you are familiar with creating a project, adding C++ code to it, compiling your code, and adding Components to Actors in the Unreal Engine editor. !!!如果你是新手,请先翻阅前面对应的译文.①We will begin by creating a new, Basic Code project, with starter content, named "HowTo_PlayerCamera". We’ll want to create a custom Pawn class, so let’s do that first. For this tutorial, we’ll use the name "PawnWithCamera" for our new Pawn class.我们先从新创建一个新的,C++基础代码,具有初学者内容的项目开始,项目命名为"HowTo_PlayerCamera".我们要创建一个Pawn类,在本教程,我们用PawnWithCamera"来命名我们新建的Pawn类.

②Next, in Visual Studio, we should openPawnWithCamera.h and add the following code to the bottom of our class definition:接下来,在VS,我们打开PawnWithCamera.h,添加如下代码到类定义的最后:protected:UPROPERTY(EditAnywhere)USpringArmComponent* OurCameraSpringArm;UCameraComponent* OurCamera; We will use these variables to create a SpringArmComponent with a CameraComponent attached to the end. Spring arms are a good, simple way to attach cameras (or other Components) so that they’re not overly rigid and feel more fluid as they move. 我们将用这些变量来创建一个SpringArmComponent(减震杆组件),并附加一个CameraComponent.减震杆是好的,简单的附加上摄像机(或者其他Components)以致于它们不会那么僵化,移动起来更流畅一些③After that, we actually need to create our Components in our constructor. Add the following code toPawnWithCamera.cpp inside of APawnWithCamera::APawnWithCamera: 之后,我们实际上需要在构造函数中创建我们的Components ,添加如下代码到PawnWithCamera.cpp ,APawnWithCamera::APawnWithCamera里面://Create our componentsRootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));//创建根组件OurCameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));//创建<span class="web-item">减震杆组件</span>OurCameraSpringArm->AttachTo(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;//延迟速度3秒 This creates a basic, empty SceneComponent as the root of our Component hierarchy, then creates and attaches a SpringArmComponent to it. The Spring Arm is then set at a default pitch of -60 degrees (that is, looking 60 degrees downward) and a position of 50 units above the root. We also establish a few values specific to the SpringArmComponent class that will determine its length and the smoothness of its motion. With that finished, we simply need to create and attach a CameraComponent to the socket on the end of the SpringArmComponent, as follows: 这将创建一个基本的,空的SceneComponent ,作为我们的组件层次结构的根,然后为它创建和附加一个SpringArmComponent .Spring Arm默认设置为倾斜角-60°,相对于root 50单位的距离.我们也可以设定一些值来指定SpringArmComponent类决定它的长度和平滑的运动,这部完成,我们只需要简单地创建和附加一个CameraComponent到插槽,在SpringArmComponent的最后,如下:OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("GameCamera"));OurCamera->AttachTo(OurCameraSpringArm, USpringArmComponent::SocketName);④Finally, we can set our Pawn to be controlled by the default, local player automatically upon being spawned by adding this piece of code:最后,我们设置Pawn为默认控制,该玩家需添加这段代码://Take control of the default PlayerAutoPossessPlayer = EAutoReceiveInput::Player0;We now have a simple Pawn that will allow us to control our camera comfortably. Next, we’ll configure our input in the Unreal Engine editor and create code that reacts to it.我们现在已经有了一个简单的Pawn ,让我们更好的控制我们的摄像机.下一步,我们要配置我们的UE编辑器的输入,创建代码,对输入作出反映.Work-In-Progress Code: PawnWithCamera.h// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.#pragma once#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 propertiesAPawnWithCamera();// Called when the game starts or when spawnedvirtual void BeginPlay() override;// Called every framevirtual void Tick( float DeltaSeconds ) override;// Called to bind functionality to inputvirtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;protected:UPROPERTY(EditAnywhere)USpringArmComponent* OurCameraSpringArm;UCameraComponent* OurCamera;};PawnWithCamera.cpp// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.#include "HowTo_PlayerCamera.h"#include "PawnWithCamera.h"// Sets default valuesAPawnWithCamera::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 componentsRootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));OurCameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));OurCameraSpringArm->AttachTo(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<UCameraComponent>(TEXT("GameCamera"));OurCamera->AttachTo(OurCameraSpringArm, USpringArmComponent::SocketName);//Take control of the default PlayerAutoPossessPlayer = EAutoReceiveInput::Player0;}// Called when the game starts or when spawnedvoid APawnWithCamera::BeginPlay(){Super::BeginPlay();}// Called every framevoid APawnWithCamera::Tick( float DeltaTime ){Super::Tick( DeltaTime );}// Called to bind functionality to inputvoid APawnWithCamera::SetupPlayerInputComponent(class UInputComponent* InputComponent){Super::SetupPlayerInputComponent(InputComponent);}2.Configure Input To Control The Camera配置输入来控制摄像机①We need to decide what our camera controls will do, and then set up inputs accordingly. For this project, let’s allow our follow distance to shorten and field of view to zoom in when the right mouse button is held down. Let’s also control our viewing angle with the mouse, and our Pawn’s movement with the WASD keys. To do this, we’ll open the Project Settings from the Edit dropdown menu in the Unreal Engine editor.我们需要确定我们的相机控制将做什么,然后建立相应的输入,在本项目,让我们遵从按下鼠标右键距离缩短和视野放大.我们也通过鼠标来控制摄像机的视角,和Pawn的通过WASD键的移动.要做这些,我们需要打开UE编辑器菜单栏的Edit(编辑)->Project Settings (项目设置)我就想是一只草原中被牧童遗忘的羊,

C++ Programming Tutorials

相关文章:

你感兴趣的文章:

标签云: