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 是实现物理碰撞的必要组件