#pragmaonce#include"CoreMinimal.h"#include"GameFramework/Pawn.h"#include"Components/BoxComponent.h"#include"Camera/CameraComponent.h"#include"Components/SplineComponent.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;//摄像机组件UPROPERTY(VisibleAnywhere)//添加到虚幻引擎中在任何地方可视
USplineComponent* Spline;//添加虚幻样条线组件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"));//创建碰撞物体名字是Collision
RootComponent = Collision;//为Actor根组件赋值(设置根节点是Collision)
Collision->InitBoxExtent(FVector(50.0f,50.0f,50.0f));//初始化碰撞体大小
Mesh =CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));//创建mesh组件名字是Mesh
Mesh->SetupAttachment(RootComponent);//把mesh的父亲设置为RootComponent
Mesh->SetWorldLocationAndRotation(FVector(50,50,50),FRotator(0,-90,0));//设置网格体的位置和旋转
Camera =CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));// 创建Camera组件名字是Camera
Camera->SetupAttachment(RootComponent);//把Camera的父亲设置为RootComponent
Camera->SetWorldLocationAndRotation(FVector(0,0,1000),FRotator(-90,0,0));//设置网格体的位置和旋转
Camera->SetAbsolute(false,true,false);//把摄像机设置为绝对坐标(位置false,旋转true,大小false)
Spline =CreateDefaultSubobject<USplineComponent>(TEXT("Spline"));//创建样条线组件名字是Spline
Spline->SetupAttachment(RootComponent);//把样条线Spline的父亲设置为RootComponent
Spline->SetAbsolute(true,true,true);//把样条线Spline设置为绝对坐标(位置true,旋转true,大小true)}// 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);}