头文件
#include "Components\StaticMeshComponent.h" 任意类
#include"Camera/CameraComponent.h"摄像机
#include "Components/InputComponent.h"按钮控制获取
#include "GameFramework\SpringArmComponent.h"角色控制
#include "UObject/ConstructorHelpers.h"//帮助类,创建东西
//声明
bUseControllerRotationYaw = true;//开启controller控制
MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
RootComponent = MyStaticMesh;//让小球不能穿过墙
MyStaticMesh->SetCollisionProfileName(TEXT("Pawn"));//设置默认名称
/* RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));//场景
MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
MyStaticMesh->SetupAttachment(GetStaticMeshComponent());*、
MySpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("MySpringArm"));
MySpringArm->SetupAttachment(MyStaticMesh);
MySpringArm->RelativeRotation = FRotator(-45.0f, 0.0f, 0.0f);//角度
MySpringArm->TargetArmLength = 400.0f;//长度
MySpringArm->bEnableCameraLag = true;//是否显示
MySpringArm->CameraLagSpeed = 3.0f;//摄像机速度
static ConstructorHelpers::FObjectFinder<UStaticMesh>StaticMeshAsset(TEXT("StaticMesh'/Engine/BasicShapes/Sphere.Sphere'"));//自动生成该类
static ConstructorHelpers::FObjectFinder<UMaterialInterface>MaterialAsset(TEXT("Material'/Game/StarterContent/Materials/M_Metal_Burnished_Steel.M_Metal_Burnished_Steel'"));
if (StaticMeshAsset.Succeeded()&& MaterialAsset.Succeeded())//判断是否成功
{
MyStaticMesh->SetStaticMesh(StaticMeshAsset.Object);
MyStaticMesh->SetMaterial(0, MaterialAsset.Object);//材质
MyStaticMesh->SetWorldScale3D(FVector(0.5f));//设置大小
}
MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));
MyCamera->SetupAttachment(GetSpringArmComponent());
MyCamera->SetRelativeLocation(FVector(-300.0f, 0.0f, 300.0f));
MyCamera->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));
AutoPossessPlayer = EAutoReceiveInput::Player0;//设置默认玩家
//X,Y,Z row pitch yaw
FRotator NewSpringArmRotation = MySpringArm->GetComponentRotation();//赋值当前旋转
NewSpringArmRotation.Pitch = FMath::Clamp(NewSpringArmRotation.Pitch += MouseInpuit.Y, -80.0f, 0.0f);//设置角度
MySpringArm->SetWorldRotation(NewSpringArmRotation);//设置成新的角度
AddControllerYawInput(MouseInpuit.X);//设置成X;
PlayerInputComponent->BindAxis(TEXT("MoveForward"), this, &AMyPawn::MoveForward);
PlayerInputComponent->BindAxis(TEXT("MoveRight"), this, &AMyPawn::MoveRight);//按键引入
FORCEINLINE UStaticMeshComponent* GetStaticMeshComponent() { return MyStaticMesh; }
FORCEINLINE USpringArmComponent* GetSpringArmComponent() { return MySpringArm; }自己创建返回函数