1 //本代碼只有cpp部分 沒有在頭文件添加任何代碼 2 // Fill out your copyright notice in the Description page of Project Settings. 3 4 #include "MyPawn.h" 5 6 #include "Runtime/Engine/Classes/Components/BoxComponent.h"// UBoxComponent 引用 7 #include "Runtime/Engine/Classes/Components/SphereComponent.h"// USphereComponent 引用 8 // Sets default values 9 AMyPawn::AMyPawn() 10 { 11 // Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it. 12 PrimaryActorTick.bCanEverTick = true; 13 14 //CreateDefaultSubobject ue4所有組件都是通過這個函數創建的 15 //<USphereComponent>與<UBoxComponent> 告訴CreateDefaultSubobject要返回對象的類型是什么 16 //虛幻4所有對象都有CreateDefaultSubobject方法 17 18 UBoxComponent *BoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("RootComponent2"));//創建一個正方體組件 RootCompont 是藍圖里面顯示的名稱 名稱隨意 19 USphereComponent* SphereComponent = this->CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));//創建一個圓球組件 RootCompont 是藍圖里面顯示的名稱 名稱隨意 20 /*Vectoe是虛幻4中的向量類*/ 21 } 22 23 // Called when the game starts or when spawned 24 void AMyPawn::BeginPlay() 25 { 26 Super::BeginPlay(); 27 28 } 29 30 // Called every frame 31 void AMyPawn::Tick(float DeltaTime) 32 { 33 Super::Tick(DeltaTime); 34 } 35 36 // Called to bind functionality to input 37 void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) 38 { 39 Super::SetupPlayerInputComponent(PlayerInputComponent); 40 41 }

spherecomponent與boxcomponenet 是實現物理碰撞的必要組件
