国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

Unreal Engine使用C++入門示例

2019-11-06 09:45:52
字體:
來源:轉載
供稿:網友

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?
01#pragma once
02 
03 
04 
05#include "GameFramework/Pawn.h"
06 
07#include "CollidingPawn.generated.h"
08 
09 
10 
11UCLASS()
12 
13class DEMO__API ACollidingPawn : public APawn
14 
15{
16 
17    GENERATED_BODY()
18 
19 
20 
21public:
22 
23    // 構造函數,可在此方法中放置物體和參數
24 
25    ACollidingPawn();
26 
27 
28 
29    // 游戲開始調用此方法
30 
31    virtual void BeginPlay() override;
32 
33     
34 
35    // 如果PrimaryActorTick.bCanEverTick設置為true,則每一幀都會調用此方法。如果處于性能的考慮,可以將其關閉。
36 
37    virtual void Tick( float DeltaSeconds ) override;
38 
39 
40 
41    // 在此函數中綁定按鍵和方法
42 
43    virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;
44 
45};

接下來,我們在原來的基礎上,來實現按鍵控制物體的小程序

先創建一個可見的球體:

在CollidingPawn.cpp的構造函數ACollidingPawn::ACollidingPawn()中添加一個球體,一個網格組件(mesh),一個彈簧臂和相機,代碼如下:

[代碼]:

view sourceprint?
01//創建一個球體
02 
03    USphereComponent* SphereComponent = CreateDefaultSubobject(TEXT("RootComponent"));
04 
05    //設置為根組件
06 
07    RootComponent = SphereComponent;
08 
09    //設置半徑
10 
11    SphereComponent->InitSphereRadius(40.0f);
12 
13    SphereComponent->SetCollisionProfileName(TEXT("Pawn"));
14 
15 
16 
17    // 創建并放置網格物體組件,這樣我們能看到球體的位置
18 
19    UStaticMeshComponent* SphereVisual = CreateDefaultSubobject(TEXT("VisualRepresentation"));
20 
21    //如果不把網格附加到SphereComponent 就看不到球體
22 
23    SphereVisual->AttachTo(RootComponent);
24 
25    static ConstructorHelpers::FObjectFinder      SphereVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
26 
27    if (SphereVisualAsset.Succeeded()){
28 
29        SphereVisual->SetStaticMesh(SphereVisualAsset.Object);
30 
31        SphereVisual->SetRelativeLocation(FVector(0.0f, 0.0f, -40.0f));
32 
33        SphereVisual->SetWorldScale3D(FVector(0.8f));
34 
35    }
36 
37 
38 
39    //  使用彈簧臂來讓相機獲得一種平滑、自然的運動。
40 
41    //  彈簧臂的目的是讓視角跟SphereComponent保持一定距離,如果不加,效果像fps游戲的第一視角一樣
42 
43    USpringArmComponent* SpringArm = CreateDefaultSubobject(TEXT("CameraAttachmentArm"));
44 
45    SpringArm->AttachTo(RootComponent);
46 
47    SpringArm->RelativeRotation = FRotator(-45.f, 0.f, 0.f); //45度角
48 
49    SpringArm->TargetArmLength = 400.0f; //彈簧臂長度
50 
51    SpringArm->bEnableCameraLag = true;
52 
53    SpringArm->CameraLagSpeed = 3.f;
54 
55 
56 
57    // 創建相機并附加到彈簧臂
58 
59    // 如果沒有相機 什么都看不到
60 
61    UCameraComponent* Camera = CreateDefaultSubobject(TEXT("ActualCamera"));
62 
63    Camera->AttachTo(SpringArm, USpringArmComponent::SocketName);

然后配置按鍵: 打開ue編輯器, 選擇編輯->項目設置-> 輸入, 然后在右邊的axis mappings加入如下設置:

MoveForWard,MoveRight,Turn,Turn_Y 可自定義,表示跟各個按鍵的綁定關系。

然后創建一個類CollidingPawnMovementComponent繼承自PawnMovementComponent(控制pawn移動的組件),我們可以把WASD綁定的行為綁定到這個component上,然后把該component綁定到我們剛才創建的球體上:

CollidingPawnMovementComponent.cpp

  

[代碼]:

view sourceprint?
01#include "Demo.h"
02 
03#include "CollidingPawnMovementComponent.h"
04 
05 
06 
07void UCollidingPawnMovementComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
08 
09{
10 
11    Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
12 
13 
14 
15    // Make sure that everything is still valid, and that we are allowed to move.
16 
17    if (!PawnOwner || !UpdatedComponent || ShouldSkipUpdate(DeltaTime))
18 
19    {
20 
21        return;
22 
23    }
24 
25 
26 
27    // Get (and then clear) the movement vector that we set in ACollidingPawn::Tick
28 
29    FVector DesiredMovementThisFrame = ConsumeInputVector().GetClampedToMaxSize(1.0f) * DeltaTime * 150.0f;
30 
31    if (!DesiredMovementThisFrame.IsNearlyZero())
32 
33    {
34 
35        FHitResult Hit;
36 
37        SafeMoveUpdatedComponent(DesiredMovementThisFrame, UpdatedComponent->GetComponentRotation(), true, Hit);
38 
39 
40 
41        // If we bumped into something, try to slide along it
42 
43        if (Hit.IsValidBlockingHit())
44 
45        {
46 
47            SlideAlongSurface(DesiredMovementThisFrame, 1.f - Hit.Time, Hit.Normal, Hit);
48 
49        }
50 
51    }
52 
53};

然后在CollidingPawn.h中加入如下代碼:

[代碼]:

view sourceprint?
1class UCollidingPawnMovementComponent* OurMovementComponent;
先將movementComponent綁定到剛才加的球體上,在CollidingPawn構造函數底部加入如下代碼:

[代碼]:

view sourceprint?
1// Create an instance of our movement component, and tell it to update the root.
2    OurMovementComponent = CreateDefaultSubobject<ucollidingpawnmovementcomponent>(TEXT("CustomMovementComponent"));
3    OurMovementComponent->UpdatedComponent = RootComponent;</ucollidingpawnmovementcomponent>
為了讓游戲中的其他類知道CollidingPawn目前正在使用CollidingPawnMovementComponent作為移動控制組件,需要在CollidingPawn.h中加入以下代碼:

[代碼]:

view sourceprint?
1virtual UPawnMovementComponent* GetMovementComponent() const override;
然后在CollidingPawn.cpp中加入:

[代碼]:

view sourceprint?
1UPawnMovementComponent* ACollidingPawn::GetMovementComponent() const
2{
3    return OurMovementComponent;
4}
剛才我們已經將新創建的移動控制組件綁定到了球體上,現在需要把WASD觸發的函數綁定到移動組件上,在CollidingPawn中實現往前移動,往左移動,轉動視角的三個方法:

[代碼]:

view sourceprint?
01// 往前(后)移動
02void ACollidingPawn::MoveForward(float AxisValue)
03{
04    if (OurMovementComponent && (OurMovementComponent->UpdatedComponent == RootComponent))
05    {
06        OurMovementComponent->AddInputVector(GetActorForwardVector() * AxisValue);
07    }
08}
09 
10// 往左(右)移動
11void ACollidingPawn::MoveRight(float AxisValue)
12{
13    if (OurMovementComponent && (OurMovementComponent->UpdatedComponent == RootComponent))
14    {
15        OurMovementComponent->AddInputVector(GetActorRightVector() * AxisValue);
16    }
17}
18 
19// 左右轉動視角
20void ACollidingPawn::Turn(float AxisValue)
21{
22    FRotator NewRotation = GetActorRotation();
23    NewRotation.Yaw += AxisValue;
24    SetActorRotation(NewRotation);
25}
然后將這三個方法在ACollidingPawn::SetupPlayerInputComponent中注冊:

[代碼]:

view sourceprint?
1void ACollidingPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
2{
3    Super::SetupPlayerInputComponent(InputComponent);
4 
5    InputComponent->BindAxis("MoveForward", this, &ACollidingPawn::MoveForward);
6    InputComponent->BindAxis("MoveRight", this, &ACollidingPawn::MoveRight);
7    InputComponent->BindAxis("Turn", this, &ACollidingPawn::Turn);
8}
以上就完成了一個可用WASD移動和鼠標控制左右視角的球體如果上下移動視角,可仿照以上的ACollidingPawn::Turn方法,將NewRotation.Yaw += AxisValue; 改為NewRotation.Pitch += AxisValue;即可

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 双流县| 安西县| 霍山县| 麟游县| 扎鲁特旗| 鄱阳县| 鄂温| 怀安县| 页游| 弥勒县| 虎林市| 焉耆| 华池县| 浙江省| 元谋县| 乌什县| 福州市| 峨眉山市| 宜宾市| 台前县| 甘孜县| 双辽市| 横山县| 文水县| 色达县| 富锦市| 平乐县| 迁安市| 德钦县| 肥东县| 潢川县| 古交市| 汾西县| 虎林市| 精河县| 雷山县| 大同县| 驻马店市| 兴海县| 沂水县| 普安县|