epic games宣布ue4免費使用之后,吸引了大批看好VR和AR前景的游戲開發者。 不過國內ue4教程和資料太少,而且一大部分資料都是關于藍圖(BluePRint)的,好在官網放出了guide和demo。更多討論,可以前往討論區:http://www.52vr.com/forum-93-1.html 本文先演示一個如何在UnrealEngine上創建一個C++項目,然后實現一個可用按鍵控制物體的示例: 1. 安裝Unreal Engine,在此不做詳細說明。從官網上下載安裝,然后注冊epic games賬號。如果要下源碼,需要綁定Github賬號,等到EpicGames自動邀請之后才能瀏覽源碼。 2. 新建項目: 安裝好之后,啟動UnrealEngine,選擇 新建項目-> c++ -> 基礎代碼 ![]()
等加載完之后,選擇文件->新建c++類,然后在如下界面選擇繼承Pawn(Pawn是可由玩家控制或者AI控制的物體的基類): ![]()
我在創建的時候取類名為CollidingPawn, 創建完之后會自動打開vs2012(如果沒裝會提示你裝一個,其他版本比如vs2015也不行,只能是2012),生成一個CollidingPawn.cpp和CollidingPawn.h。 頭文件如下所示, 我按照自己的理解加了注釋: CollidingPawn.h: [代碼]:view sourceprint?05 | #include "GameFramework/Pawn.h" |
07 | #include "CollidingPawn.generated.h" |
13 | class DEMO__API ACollidingPawn : public APawn |
31 | virtual void BeginPlay() override ; |
37 | virtual void Tick( float DeltaSeconds ) override ; |
43 | virtual void SetupPlayerInputComponent( class UInputComponent* InputComponent) override ; | 接下來,我們在原來的基礎上,來實現按鍵控制物體的小程序 先創建一個可見的球體: 在CollidingPawn.cpp的構造函數ACollidingPawn::ACollidingPawn()中添加一個球體,一個網格組件(mesh),一個彈簧臂和相機,代碼如下: [代碼]:view sourceprint?03 | USphereComponent* SphereComponent = CreateDefaultSubobject(TEXT( "RootComponent" )); |
07 | RootComponent = SphereComponent; |
11 | SphereComponent->InitSphereRadius(40.0f); |
13 | SphereComponent->SetCollisionProfileName(TEXT( "Pawn" )); |
19 | UStaticMeshComponent* SphereVisual = CreateDefaultSubobject(TEXT( "VisualRepresentation" )); |
23 | SphereVisual->AttachTo(RootComponent); |
25 | static ConstructorHelpers::FObjectFinder SphereVisualAsset(TEXT( "/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere" )); |
27 | if (SphereVisualAsset.Succeeded()){ |
29 | SphereVisual->SetStaticMesh(SphereVisualAsset.Object); |
31 | SphereVisual->SetRelativeLocation(FVector(0.0f, 0.0f, -40.0f)); |
33 | SphereVisual->SetWorldScale3D(FVector(0.8f)); |
43 | USpringArmComponent* SpringArm = CreateDefaultSubobject(TEXT( "CameraAttachmentArm" )); |
45 | SpringArm->AttachTo(RootComponent); |
47 | SpringArm->RelativeRotation = FRotator(-45.f, 0.f, 0.f); |
49 | SpringArm->TargetArmLength = 400.0f; |
51 | SpringArm->bEnableCameraLag = true ; |
53 | SpringArm->CameraLagSpeed = 3.f; |
61 | UCameraComponent* Camera = CreateDefaultSubobject(TEXT( "ActualCamera" )); |
63 | Camera->AttachTo(SpringArm, USpringArmComponent::SocketName); |
然后配置按鍵: 打開ue編輯器, 選擇編輯->項目設置-> 輸入, 然后在右邊的axis mappings加入如下設置: 
MoveForWard,MoveRight,Turn,Turn_Y 可自定義,表示跟各個按鍵的綁定關系。 然后創建一個類CollidingPawnMovementComponent繼承自PawnMovementComponent(控制pawn移動的組件),我們可以把WASD綁定的行為綁定到這個component上,然后把該component綁定到我們剛才創建的球體上: CollidingPawnMovementComponent.cpp
[代碼]:view sourceprint?03 | #include "CollidingPawnMovementComponent.h" |
07 | void UCollidingPawnMovementComponent::TickComponent( float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) |
11 | Super::TickComponent(DeltaTime, TickType, ThisTickFunction); |
17 | if (!PawnOwner || !UpdatedComponent || ShouldSkipUpdate(DeltaTime)) |
29 | FVector DesiredMovementThisFrame = ConsumeInputVector().GetClampedToMaxSize(1.0f) * DeltaTime * 150.0f; |
31 | if (!DesiredMovementThisFrame.IsNearlyZero()) |
37 | SafeMoveUpdatedComponent(DesiredMovementThisFrame, UpdatedComponent->GetComponentRotation(), true , Hit); |
43 | if (Hit.IsValidBlockingHit()) |
47 | SlideAlongSurface(DesiredMovementThisFrame, 1.f - Hit.Time, Hit.Normal, Hit); | 然后在CollidingPawn.h中加入如下代碼:[代碼]:view sourceprint?1 | class UCollidingPawnMovementComponent* OurMovementComponent; | 先將movementComponent綁定到剛才加的球體上,在CollidingPawn構造函數底部加入如下代碼:[代碼]:view sourceprint?2 | OurMovementComponent = CreateDefaultSubobject<ucollidingpawnmovementcomponent>(TEXT( "CustomMovementComponent" )); |
3 | OurMovementComponent->UpdatedComponent = RootComponent;</ucollidingpawnmovementcomponent> | 為了讓游戲中的其他類知道CollidingPawn目前正在使用CollidingPawnMovementComponent作為移動控制組件,需要在CollidingPawn.h中加入以下代碼:[代碼]:view sourceprint?1 | virtual UPawnMovementComponent* GetMovementComponent() const override ; | 然后在CollidingPawn.cpp中加入:[代碼]:view sourceprint?1 | UPawnMovementComponent* ACollidingPawn::GetMovementComponent() const |
3 | return OurMovementComponent; | 剛才我們已經將新創建的移動控制組件綁定到了球體上,現在需要把WASD觸發的函數綁定到移動組件上,在CollidingPawn中實現往前移動,往左移動,轉動視角的三個方法:[代碼]:view sourceprint?02 | void ACollidingPawn::MoveForward( float AxisValue) |
04 | if (OurMovementComponent && (OurMovementComponent->UpdatedComponent == RootComponent)) |
06 | OurMovementComponent->AddInputVector(GetActorForwardVector() * AxisValue); |
11 | void ACollidingPawn::MoveRight( float AxisValue) |
13 | if (OurMovementComponent && (OurMovementComponent->UpdatedComponent == RootComponent)) |
15 | OurMovementComponent->AddInputVector(GetActorRightVector() * AxisValue); |
20 | void ACollidingPawn::Turn( float AxisValue) |
22 | FRotator NewRotation = GetActorRotation(); |
23 | NewRotation.Yaw += AxisValue; |
24 | SetActorRotation(NewRotation); | 然后將這三個方法在ACollidingPawn::SetupPlayerInputComponent中注冊:[代碼]:view sourceprint?1 | void ACollidingPawn::SetupPlayerInputComponent( class UInputComponent* InputComponent) |
3 | Super::SetupPlayerInputComponent(InputComponent); |
5 | InputComponent->BindAxis( "MoveForward" , this , &ACollidingPawn::MoveForward); |
6 | InputComponent->BindAxis( "MoveRight" , this , &ACollidingPawn::MoveRight); |
7 | InputComponent->BindAxis( "Turn" , this , &ACollidingPawn::Turn); | 以上就完成了一個可用WASD移動和鼠標控制左右視角的球體如果上下移動視角,可仿照以上的ACollidingPawn::Turn方法,將NewRotation.Yaw += AxisValue; 改為NewRotation.Pitch += AxisValue;即可 |