设置物体初始位置
.h文件中做声明
UPROPERTY(EditInstanceOnly,Category="My Actor Properties")
FVector InitLocation;
.cpp中实现
InitLocation=FVector(0.0f);
SetActorLocation(InitLocation)
新学到的蓝图宏:EditInstanceOnly
只能在视口界面的面板中编辑物体的位置,不能在蓝图类中编辑
设置物体初始位置
.h文件中做声明
UPROPERTY(EditInstanceOnly,Category="My Actor Properties")
FVector InitLocation;
.cpp中实现
InitLocation=FVector(0.0f);
SetActorLocation(InitLocation)
新学到的蓝图宏:EditInstanceOnly
只能在视口界面的面板中编辑物体的位置,不能在蓝图类中编辑
课时15 变换工具
W平移 E旋转 R缩放
如何控制每次物体移动的量?
答:调整视口右上角从左数第一个数值,取消捕捉移动也行
如何让摄像机以我们要移动的物体为参考系?
答:移动物体时按住shift键
UPROPERTY();的宏参数
EditInstanceOnly 只能在实例中设置
VisibleInstanceOnly 只能在实例中可见
EditDefaultsOnly 只能在蓝图类中设置
VisibleDefaultsOnly 只能在蓝图类中可见
EditAnywhere 任何地方可设置
VisibleAnywhere 任何地方可见
ue4单位 1 等于现实生活一厘米
.h是写声明的
.cpp是写逻辑的
Blueprintable(蓝图继承)
EditInstanceOnly
SetActorLocation(FVector(0.0f));
UCLASS(Blueprintable)
这一节课废话有点多,不知道什么时候不如正题
蓝图和C++丽影
UCLASS
UPROPERTY
UFUNCTION
//
在#include"XXXX.generated.h"之后不要再include任何东西
//
UHT是什么
511
1.打开使用控制器的旋转:
//Z轴方向的旋转,左右旋转
bUseControllerRotationYaw=true;
2.在Tick函数中添加:
AddControllerYawInpout(MouseInput.X);
510
1.添加按键映射:
LookUp:MouseY
LookRight:MouseX
2.在头文件中声明:
void LookUp(float Value);
void LookRight(float Value);
FVector2D MouseInput;
3.在源文件中实现:
//按键映射绑定
PlayerInputComponent->BindAxis(TEXT("LookUp"),this,&AMyPawn::LookUp);
PlayerInputComponent->BindAxis(TEXT("LookRight"),this,&AMyPawn::LookRight);
//实现
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);
}
//在Tick函数中:
FRotator NewSpringArmRotation = MySpringArm->GetComponentRotation();
NewSpringArmRotation.Pitch=FMath::Clamp(NewSpringArmRotation.Pitch+=MouseInput.Y,-80.0f,0.0f);
MySpringArm->SetWorldRotator(NewSpringArmRotation);
508
1.优化代码,设置get方法:
//FORCEINLINE是UE中的一个宏,用于优化,用在get/set方法之前或者方法体中只有一条return语句
FORCEINLINE UStaticMeshComponent* GetStaticMeshComponent(){return MyStaticMesh;}
FORCEINLINE USpringArmComponent* GetSpringArmComponent(){return MySpringArm;}
2.在CPP中替换上述两个方法
3.加载静态网格、材质文件:
#include "UObject/ConstructorHelpers.h"
//其中的XXX为在content brower中找到对应的网格模型和材质的文件,选中点击右键拷贝他们的文件引用路径
static ConstructorHelpers::FObjectFinder<UStaticMesh>StaticMeshAsset(TEXT("xxx"));
static ConstructorHelpers::FObjectFinder<UMaterialInterface>MaterialAsset(TEXT("xxx"));
if(StaticMeshAsset.Succeeded() && MaterialAsset.Succeeded()){
MyStaticMesh->SetStaticMesh(StaticMeshAsset.Object);
MyStaticMesh->SetMaterial(0,MaterialAsset.Object);
MyStaticMesh->SetWorldScale3D(FVector(0.5f));
}
507
1.在MyPawn.h中添加弹簧臂组件声明:
public:
UPROPERTY(VisibleAnywhere,Category="My Pawn Components")
class USpringArmComponent* MySpringArm;
2.在MyPawn.cpp中包含头文件:
#include "GameFramework/SpringArmComponent.h"
3.在MyPawn.cpp中添加:
MySpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("MySpringArm"));
MySpringArm ->SetupAttachment(MyStaticMesh);
MySpringArm ->RelativeRotation = FRotator(-45.0f,0.0f,0.0f);
MySpringArm ->TargetArmLength=400.f;
MySpringArm ->bEnableCameraLag=true;
MySpringArm ->CameraLagSpeed=3.0f;
4.注释掉摄像机设置位置和旋转的代码
5.将摄像机附着到弹簧臂组件上:
MyCamera->SetupAttachment(MySpringArm);
506
1.deltatime:两帧之间的时间(屏幕刷新时间),根据电脑的性能决定,高配置的电脑小,低配置的电脑大
两个玩家,假设Tick中增量1单位:
P1 10FPS,deltatime=1/10=0.1s
P2 5FPS,deltatime=1/5 = 0.2s
不使用deltatime:
P1 10*1=10,1秒内移动了10个单位
P2 5*1= 5,1秒内移动了 5个单位
使用deltatime:
P1 10*1*0.1=1,1秒内移动了1个单位
P2 5*1*0.2=1,1秒内移动了1个单位
根据上述比较结果可以看出,deltatime用于解决不同玩家因为电脑性能不同导致游戏中的物体在同一段时间内移动的距离不一样的问题
2.按键~调出控制台,输入t.MaxFPS 10可以调节FPS的值
505
1.在头文件中增加成员变量声明:
public:
//增加一个移动速度变量用于可以在外部进行编辑
public:
UPROPERTY(EditAnywhere,Category="My Pawn Movement")
float MaxSpeed;
private:
//内部使用的一个位置偏移向量
FVector Volacity;
2.在源文件中进行初始化:
MaxSpeed=100.0f;
Volacity=FVector(0.0f);
3.在源文件中修改MoveForward和MoveRight函数:
void MyPawn::MoveForward(float Value){
Volacity.X=FMath::Clamp(Value,-1.0f,1.0f)*MaxSpeed;
}
void MyPawn::MoveRight(float Value){
Volacity.Y=FMath::Clamp(Value,-1.0f,1.0f)*MaxSpeed;
}
4.在源文件的Tick函数中添加:
AddActorLocalOffset(Volacity*deltatime,true);
5.编译测试
504
1.添加按键映射,在project setting -> input -> Axis Mapping:
MoveForward:W,S
MoveRight:A,D
2.在头文件中声明两个成员函数:
void MoveForward(float Value);
void MoveRight(float Value);
3.在源文件中包含头文件:
#include "Components/InputComponent.h"
4.在源文件中空实现上述两个函数:
void MyPawn::MoveForward(float Value){
}
void MyPawn::MoveRight(float Value){
}
5.在源文件的SetupPlayerInputComponent函数中添加:
PlayerInputComponent->BindAxis(TEXT("MoveForward"),this,&MyPawn::MoveForward);
PlayerInputComponent->BindAxis(TEXT("MoveRight"),this,&MyPawn::MoveRight);
503
1.创建BP_GameMode继承GameMode
2.打开BP_GameMode,将default Pawn改为BP_ MyPawn
3.在世界设置中替换为自己的BP_GameMode
4.在源文件构造函数中添加代码:
AutoPossessPlayer = EAutoReceiveInput::Player0;