UE4基础,Actor设置与摄像机SpringArm添加

news/2024/11/7 22:38:50/

基础知识整理

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);
}

http://www.ppmy.cn/news/206445.html

相关文章

(52)组件之摄像机组件

CameraComponent &#xff08;添加一个摄像机视角&#xff09;和 SpringArmComponent &#xff08;使其子项延长固定距离&#xff0c;然后在发生碰撞时收回&#xff09;&#xff0c;这两个组件一起使用&#xff0c;可提供一个第三人称视角&#xff0c;您可在游戏世界中对其进行…

UE4学习总结(5) 相机基础

UE4学习总结&#xff08;5&#xff09; 相机基础 相机种类控制自由相机定义Input事件配置各个按键的控制效果转换到相机的视角 控制摇臂相机摇臂相机搭载子相机配置各个按键的控制效果 控制滑轨相机滑轨相机搭载子相机控制相机位置控制滑轨相机对准目标转换到相机的视角 控制多…

时光剪影

关于我们 成都时光剪影文化传播有限公司&#xff0c;简称SGJY&#xff0c;是中国一家影视制作机构&#xff0c;总部位于成都。 SGJY&#xff08;时光剪影&#xff09;主要致力于服务全球影视制作业务&#xff0c;涵盖广告片、创意视频短片、强传播型微电影、宣传片、新型教学片…

【UE Sequencer系列】08-副镜头切换、摄像机绑定摇臂的使用、摄像机绑定滑轨的使用

目录 一、副镜头切换 二、摄像机绑定摇臂的使用 三、摄像机绑定滑轨的使用 一、副镜头切换 1. 为“shot_05”新建镜头&#xff08;复制资产&#xff0c;创建新的关卡序列&#xff09; 同样的步骤再创建一个“Shot_07_02” 此时我们就可以对“Shot_07”中的两个副镜头进行切换…

C4D 摄像机与动画 学习笔记

C4D 摄像机与动画 学习笔记 一、工程设置 模式--工程 帧率25 二、关键帧 删除关键帧 关键帧为黄色时为未选中&#xff0c;为红色时是设置关键帧 ctrlshift 点击关键帧&#xff08;删除关键帧&#xff09; 右击关键帧-动画--删除轨迹&#xff08;删除所有关键帧&#xff…

c4d-摄像机工具与环境工具-8

文章目录 摄像机对象 目标摄像机立体摄像机运动像机摄像机变换摇臂像机环境地面和天空前景和背景场景真实天空云绘制工具云组 摄像机 对象 普通摄像机 投射方式 摄像机的选型 焦距 影响视野&#xff0c;比如通常的广角镜头显得雄伟等等&#xff0c;有些效果调调焦距就能得到效…

运动摄像的运镜方式

运动摄像&#xff0c;就是在一个镜头中通过移动摄像机机位&#xff0c;或者改变镜头光轴&#xff0c;或者变化镜头焦距所进行的拍摄。通过这种拍摄方式所拍到的画面&#xff0c;称为运动画面。如&#xff1a;由推、拉、摇、移、跟、升降摄像和综合运动摄像形成的推镜头、拉镜头…

【UE4 RTS游戏】02-摄像机运动_完成摄像机在X轴上运动的相关步骤

效果 通过控制键盘WS键使得“CameraPawn”进行前后移动 步骤 将landscape的Z轴位置更改为0 删除“PostProcessVolume” 将“LightmassImportanceVolume”移入Lighting文件夹内 新建一个蓝图类&#xff0c;父类是Pawn&#xff0c;命名为“CameraPawn” 将“MyController”重命名…