#pragmaonce#include"CoreMinimal.h"#include"GameFramework/Pawn.h"#include"Components/BoxComponent.h"#include"Camera/CameraComponent.h"#include"SnakeHead.generated.h"UCLASS()classSNAKEGAME_API ASnakeHead :public APawn
{GENERATED_BODY()public:// Sets default values for this pawn's propertiesASnakeHead();UPROPERTY(VisibleAnywhere)//添加到虚幻引擎中在任何地方可视UStaticMeshComponent* Mesh;//mesh组件UPROPERTY(VisibleAnywhere)//添加到虚幻引擎中在任何地方可视UBoxComponent* Collision;//碰撞组件UPROPERTY(VisibleAnywhere)//添加到虚幻引擎中在任何地方可视UCameraComponent* Camera;//摄像机组件protected:// Called when the game starts or when spawnedvirtualvoidBeginPlay()override;public:// Called every framevirtualvoidTick(float DeltaTime)override;// Called to bind functionality to inputvirtualvoidSetupPlayerInputComponent(classUInputComponent* PlayerInputComponent)override;};
UE.cpp文件:
// Fill out your copyright notice in the Description page of Project Settings.#include"SnakeHead.h"// Sets default valuesASnakeHead::ASnakeHead(){// 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;Collision =CreateDefaultSubobject<UBoxComponent>(TEXT("Collision"));//创建碰撞物体名字是CollisionRootComponent = Collision;//为Actor根组件赋值Mesh =CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));//创建mesh组件名字是MeshMesh->SetupAttachment(RootComponent);//把mesh的父亲设置为RootComponentCamera =CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));// 创建Camera组件名字是CameraCamera->SetupAttachment(RootComponent);//把Camera的父亲设置为RootComponent}// Called when the game starts or when spawnedvoidASnakeHead::BeginPlay(){Super::BeginPlay();}// Called every framevoidASnakeHead::Tick(float DeltaTime){Super::Tick(DeltaTime);}// Called to bind functionality to inputvoidASnakeHead::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent){Super::SetupPlayerInputComponent(PlayerInputComponent);}