基础知识整理
Uobject子类蓝图的创建
//.h
UCLASS(Blueprintable) //使其能在UE中可被蓝图继承,反射系统
class PROJECT01_API UMyobject : public UObject
{GENERAtED_BODY()
public:UMyobject();//不参与反射系统UPROPERTY(BlueprintReadWrite, Category = "My Variables")//蓝图中可被读写float MyFloat;UFUNCTION(BlueprintCallable, Category = "My Functions")//蓝图中可被调用void MyFunction();}
//.cpp
#include "MyObject.h"UMyObject::UMyObject()
{MyFloat = 0.0f;
}void UMyObject::MyFunction()
{//UE中输出信息UE_LOG(LogTemp, Log, TEXT("Hello World !"));UE_LOG(LogTemp, Warning, TEXT("Hello World !"));UE_LOG(LogTemp, Error, TEXT("Hello World !"));
}
在UE中实例化继承于C++Object的蓝图
在关卡蓝图中,Event BeginPlay->Construct Object from Class->选择继承的蓝图类,Outer同样的类->Return Value右键Promote to Variable提升为变量->调用Function
删除自定义C++类
工程文件夹->Source->Basic Training->删除类.h.cpp
工程文件夹->删除Binaries
工程文件夹->.uproject文件右键->Generate Visual Studio project files
Actor子类蓝图的创建
//.h
UCLASS()
class PROJECT01_API AMyActor : public AActor
{GENERATED_BODY()public:AMyAcotr();UPROPERTY(VisibleAnywhere, Category = "My Actor Components")//可在任何地方可见UStaticMeshComponent* StaticMesh;UPROPERTY(EditInstanceOnly, Category = "My Actor Properties | Vector")//仅在实例中可以编译,在模板中不可编辑FVector InitLocation;UPROPERTY(VisibleInstanceOnly, Category = "My Actor Properties | Vector")//仅在实例中可以见,在模板中不可见FVector PlaceLocation;UPROPERTY(EditDefaultsOnly, Category = "My Actor Properties | Vector")//仅在模板中可以编译,在实例中不可编辑bool bGotoInitLocationUPROPERTY(VisibleDefaultsOnly, Category = "My Actor Properties | Vector")//仅在模板中可以见,在实例中不可见FVector WorldOrigin;UPROPERTY(EditAnywhere, Category = "My Actor Properties | Vector". meta = (ClampMin = -5.0f,ClampMax = 5.0f,UIMin = 5.0f,UIMax = 0.5f))//可在任何地方编译FVector TickLocationOffset;UPROPERTY(EditAnywhere, Category = "My Actor Properties | Vector")//可在任何地方编译bool bShouldMove;protected:virtual void BeginPlay() override;
public:virtual void Tick(float DeltaTime) override;
}
//.cpp
#include "MyActor.h"
AMyActor::AMyActor()
{PrimaryActorTick.bCanEverTick = true;StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh "));//TEXT在系统提示和报错中调用TEXTInitLocation = FVector(0.0f);PlaceLocation = FVector(0.0f);bGotoInitLocation = false;WorldOrigin = FVector(0.0f);TickLoactionOffset = FVector(0.0f);bShouldMove = false;
}void AMyActor::BeginPlay()
{Super::BeginPlay();PlaceLocation = GetActorLocation();if(bGotoInitLocation){SetActorLocation(InitLocation);}}void AMyActor::Tick(float DeltaTime)
{Super::Tick(DeltaTime);if(bShouldMove){AddActorLocalOffset(TickLocationOffset);}
}
视野启动位置的设置
调整需要开始的视角->选择场景中PlayerStart右键->Snap Object to View
游戏运行时调用命令控制台查看碰撞器
运行时按下~键,输入show Collision
给物体增加力和力矩,移动与旋转
//.h
UCLASS()
class PROJECT01_API AMyActor : public AActor
{GENERATED_BODY()public:AMyAcotr();UPROPERTY(EditInstanceOnly, Category = "My Actor Properties | Physics")//仅在实例中可以编译,在模板中不可编辑FVector InitForce;UPROPERTY(EditInstanceOnly, Category = "My Actor Properties | Physics")//仅在实例中可以编译,在模板中不可编辑FVector InitTorque;UPROPERTY(EditInstanceOnly, Category = "My Actor Properties | Physics")//仅在实例中可以编译,在模板中不可编辑FVector bAccelChange;//是否忽略物体质量
protected:virtual void BeginPlay() override;
public:virtual void Tick(float DeltaTime) override;
}
//.cpp
#include "MyActor.h"
#include “Components/StaticMeshComponent.h"
AMyActor::AMyActor()
{PrimaryActorTick.bCanEverTick = true;StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh "));//TEXT在系统提示和报错中调用TEXTInitForce = FVector(0.0f);InitTorque = FVector(0.0f);bAccelChange = false;
}void AMyActor::BeginPlay()
{Super::BeginPlay();StaticMesh-> AddForce(InitForce,NAME_None,bAccelChange);StaticMesh-> AddTorque(InitTorque,NAME_None,bAccelChange);
}void AMyActor::Tick(float DeltaTime)
{Super::Tick(DeltaTime);
}
不启动物理使物体发生移动碰撞,与碰撞点检测
FHitResult HitResult;
AddActorLocalOffset(Vector, true, &HitResult);//第一个参数为每一帧移动的矢量距离。
//第二个参数为Sweep扫描移动路径上是否有物体,产生遮挡效果。第三个参数记录并返回产生碰撞结果。
UE_LOG(LogTemp, Warning, TEXT("X: %f, Y: %f, Z: %f"),HitResult.Location.X, HitResult.Location.Y, HitResult.Location.Z);
Pawn类的移动和摄像机SpringArm的添加
public://Sets default value for this pawn's propertiesAMyPawn();UPROPERTY(VisibleAnyWhere,Category = "My Pawn Components")UStaticMeshComponent* MyStaticMesh;UPROPERTY(VisibleAnyWhere,Category = "My Pawn Components")UCameraComponent* MyCamera;UPROPERTY(VisibleAnyWhere,Category = "My Pawn Components")USpringArmComponent* MySpringArm;UPROPERTY(EditAnyWhere,Category = "My Pawn Components")float MaxSpeed;FORCEINLINE UStaticMeshComponent* GetStaticMeshComponent(){return MyStaticMesh;}FORCEINLINE USpringArmComponent* GetSpringArmComponent(){return MySpringArm;}protected://Called When the game starts or when spawnedvirtual void BeginPlay() override;public://Called every framevirtual void Tick(float DeltaTime)override;//Called to bind functionality to inputvirtual void SetupPlayerInputpComponent(class UInputComponent* PlayerInputComponent)override;private:void MoveForward(float Value);void MoveLeft(float Value);FVector Velocity;void LookUp(float Value);void LookRight(float Value);FVector2D MouseInput;
#include "GameFramework/SpringArmComponent.h"
#include "UObject/ConstructorHelpers.h"AMyPawn::AMyPawn()
{PrimaryActorTick.bCanEverTick = true;//声明MeshMyStaticMesh = CreareDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));//将mesh作为根组件RootComponent = MyStaticMesh;MyStaticMesh->SetCollisionProfileName(TEXT("Pawn"));//加载Mesh和Materialstatic ConstructorHelpers::FObjectFinder<UStaticMesh>StaticMeshAsset(TEXT("Mesh文件Reference");static ConstructorHelpers::FObjectFinder<UMaterialInterface>MaterialAsset(TEXT("Material文件Reference");if(StaticMeshAsset.Successded() && MaterialAsset.Successeded()){MyStaticMesh->SetStaticMesh(StaticMeshAsset.Object);MyStaticMesh->SetMaterial(0,MaterialAsset.Object);MyStaticMesh->SetWorldScale3D(FVector(0.5f));}//创建摇臂MySpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("MySpringArm"));//声明MySpringArm->SetupAttachment(GetStaticMeshComponent());//挂到物体上MySpringArm->AddRelativeRotation(FRotator(-45.0f,0.0f,0.0f);//设置角度MySpringArm->TragetArmLength = 400.0f;//长度MySpringArm->bEnableCameraLag = true;//启用平滑跟随MySpringArm->CameraLagSpeed = 3.0f;//设置跟随速度//设置并把摄像机绑定到摇臂MyCamera = CreateDefualSubobject<UCameraComponent>(TEXT(“MyCamera"));MyCamera->SetupAttachment(GetSpringArmComponent());AutoPossessPlayer = EAutoReceiveInput::Player0;bUseControllerRotationYaw = true;MaxSpeed = 100.0f;Velocity = FVector::ZeroVector;
}void AMyPawn::BeginPlay()
{Super::BeginPlay();
}void AMyPawn::Tick(float DeltaTime)
{Super::Tick(DeltaTime);//物体移动AddActorLocalOffset(Velocity * DeltaTime, true);//bSweep开启碰撞扫描//左右转方向旋转AddControllerYawInput(MouseInput.X);//视角左右移动,不需要限制不用夹值,物体朝向转动,摇臂和摄像机跟随。//上下视角旋转FRotator NewSpringArmRotation = MySpringArm->GetComponentRotation();//拿到摇臂RotationNewSpringArmRotation.Pitch = FMath::Clamp(NewSpringArmRotation.Pith += MouseInput.Y, -80.0f,0.0f);//夹值调整摇臂旋转,限定视角MySpringArmRotation->SetWorldRotation(NewSpringArmRotation);//摇臂赋值,物体不动
}void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{Super::SetupPlayerInputComponent(PlayerInputComponent);PlayerInputComponent->BinAxis(TEXT("MoveForward"),this,&AMyPawn::MoveForward);//对应Input Setting词条,作用于自身,调用变量设置函数PlayerInputComponent->BinAxis(TEXT("MoveLeft"),this,&AMyPawn::MoveLeft);PlayerInputComponent->BinAxis(TEXT("LookUp"),this,&AMyPawn::LookUp);PlayerInputComponent->BinAxis(TEXT("LookRight"),this,&AMyPawn::LookRight);
}void AMyPawn::MoveForward(float Value)
{Velocity.X = FMath::Clamp(Value, -1.0f, 1.0f) * MaxSpeed;
}void AMyPawn::MoveLeft(float Value)
{Velocity.X = FMath::Clamp(Value, -1.0f, 1.0f) * MaxSpeed;
}void AMyPawn::Lookup(float Value)
{MouseInput.Y = FMath::Clamp(Value ,-1.0f, 1.0f);
}void AMyPawn::LookRight(float Value)
{MouseInput.X = FMath::Clamp(Value ,-1.0f, 1.0f);
}