ue4c++日记4(控制pawn类的运动|创建游戏模式|)

目录

代码速查

调用数学公式

 获取位置/设置位置

 绑定玩家输入按键,UE4传值给函数进行处理

约束获得的值再输出

创建对象

 对象绑定到xxx上

设定默认玩家 

 实例:sin函数实现往复运动

实例:删除c++类

1.删掉cpp和.h文件

2.删编译好的文件Binaries

 3.重新生成vs文件

 4.ok

pawn的c++(模型和照相机模型)

创建游戏模式

新建自己的游戏模式。 

更改默认pawn类

 更改游戏模式(游戏模式重载)

控制pawn类的移动

(在上面的“pawn的c++(模型和照相机模型)”的基础上上)

1.头文件声明变量 

2.代码文件

3.做一套轴映射


代码速查

调用数学公式

FMath::

 获取位置/设置位置

GetActorLocation
SetActorLocation

 绑定玩家输入按键,UE4传值给函数进行处理

/// <summary>
	/// 绑定玩家输入
	/// 橙色字对应UE4的轴映射的字
	/// this是这个对象
	/// 后面是UE4按键响应传值给到这个方法
	/// </summary>
	PlayerInputComponent->BindAxis("MoveForward", this, &AMyPawn::MoveForward);

约束获得的值再输出

FMath::Clamp(Value, -1.f, 1.f) 
例:CurrentVelocity.X = FMath::Clamp(Value, -1.f, 1.f) * MaxSpeed;

创建对象

 

CreateDefaultSubobject<组件>

RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));//设定根组件

 对象绑定到xxx上

SetupAttachment
例:Camera->SetupAttachment(GetRootComponent());

设定默认玩家 

	AutoPossessPlayer = EAutoReceiveInput::Player0;//0号玩家

 实例:sin函数实现往复运动

	RunningTime += DeltaTime;
	FVector NewLocation = GetActorLocation();
	NewLocation.Z += FMath::Sin(RunningTime);
	SetActorLocation(NewLocation);

实例:删除c++类

1.删掉cpp和.h文件

2.删编译好的文件Binaries

 3.重新生成vs文件

 

 点是

 4.ok

pawn的c++(模型和照相机模型)

头文件public

	UPROPERTY(EditAnywhere)
		class UStaticMeshComponent* MyMesh;//模型
	UPROPERTY(EditAnywhere)
		class UCameraComponent* Camera;//摄像机

cpp构造函数

上半段构造静态网格体绑定在根组件上

下半段构造照相机绑定在根组件上

	RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));//设定根组件
	MyMesh = CreateDefaultSubobject<UStaticMeshComponent >(TEXT("Mesh"));//设定一个静态网格体
	MyMesh->SetupAttachment(GetRootComponent());//将静态网格体绑定到根组件下


	Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
	Camera->SetupAttachment(GetRootComponent());
	Camera->SetRelativeLocation(FVector(0,0,300));
	Camera->SetRelativeRotation(FRotator(-45.f,0,0));

创建游戏模式

(好像是自带的游戏模式)

新建自己的游戏模式。 

更改默认pawn类

在上面创建的游戏模式中更改pawn类

 

 更改游戏模式(游戏模式重载)

 做出来的结果是:点运行后使用的是摄像机的镜头看。

控制pawn类的移动

(在上面的“pawn的c++(模型和照相机模型)”的基础上上)

1.头文件声明变量 

2.代码文件


// Called every frame
void AMyPawn::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	SetActorLocation(GetActorLocation() + CurrentVelocity * DeltaTime);//设定对象位置
}

// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
	/// <summary>
	/// 绑定玩家输入
	/// 橙色字对应UE4的轴映射的字
	/// this是这个对象
	/// 后面是UE4按键响应传值给到这个方法
	/// </summary>
	PlayerInputComponent->BindAxis("MoveForward", this, &AMyPawn::MoveForward);
	PlayerInputComponent->BindAxis("MoveRight", this, &AMyPawn::MoveRight);
}

void AMyPawn::MoveForward(float Value)
{
	//clamp约束数值在-1到1
	CurrentVelocity.X = FMath::Clamp(Value, -1.f, 1.f) * MaxSpeed;
}

void AMyPawn::MoveRight(float Value)
{
	CurrentVelocity.Y = FMath::Clamp(Value, -1.f, 1.f) * MaxSpeed;
}

3.做一套轴映射

例:对应PlayerInputComponent->BindAxis("MoveForward", this, &AMyPawn::MoveForward);的"MoveForward"

猜你喜欢

转载自blog.csdn.net/weixin_56537692/article/details/128726313