UE4入门序列03(C++制作一个跑酷游戏 part 3/3)


这是一个功能简版Demo,主要用来熟悉C++代码!

#1 生成的地板修复
#2 框架及注意事项
#3 欢迎下载源码阅读


UE4入门序列03(C++制作一个跑酷游戏 part 3/3)_第1张图片


#1 生成的地板修复

// Free demo project  for tutorials,  RunnerDemo


#include "RunnerGameMode.h"


#include "RunnerDemo.h"
#include "Kismet/GameplayStatics.h"

void ARunnerGameMode::BeginPlay()
{
     
	InitFloor();
}

void ARunnerGameMode::InitFloor()
{
     
	AddonVector = FVector::ForwardVector * 1250.0f;
	for (int i = 0; i < 5; ++i)
	{
     
		FVector SpawnLocation = LatestFloor ? LatestFloor->GetActorLocation() + AddonVector : FVector::ZeroVector;
		FRotator SpawnRotation = LatestFloor ? LatestFloor->GetActorRotation() : FRotator::ZeroRotator;
		ARunnerFloor* SpawnedActor = GetWorld()->SpawnActor<ARunnerFloor>(FloorToSpawn, SpawnLocation, SpawnRotation);
		
		if (i == 3 && LatestFloor)
		{
     
			SpawnedActor->AddActorLocalRotation(FRotator(0.0f, -90.0f, 0.0f));
			FVector ActorForwardVector = SpawnedActor->GetActorForwardVector();
			AddonVector = ActorForwardVector * 1250.0f;
			
		}
		if (i == 4 && LatestFloor)
		{
     
			SpawnedActor->AddActorLocalRotation(FRotator(0.0f, 90.0f, 0.0f));
			FVector ActorForwardVector = SpawnedActor->GetActorForwardVector();
			AddonVector = ActorForwardVector * 1250.0f;
			
		}

		LatestFloor = SpawnedActor;
	}
	
	
}

void ARunnerGameMode::Tick(float DeltaSeconds)
{
     
}

void ARunnerGameMode::NewFloor()
{
     
	float Rate = FMath::RandRange(0.0f, 1.0f);
	
	FVector SpawnLocation = LatestFloor ? LatestFloor->GetActorLocation() + AddonVector : FVector::ZeroVector;
	FRotator SpawnRotation = LatestFloor ? LatestFloor->GetActorRotation() : FRotator::ZeroRotator;
	ARunnerFloor* SpawnedFloor = GetWorld()->SpawnActor<ARunnerFloor>(FloorToSpawn, SpawnLocation, SpawnRotation);
	
	if (Rate < 0.2f)
	{
     
		bool bRight = FMath::RandBool();
		float ToYaw = bRight ? 90.0f : -90.0f;
		SpawnedFloor->AddActorLocalRotation(FRotator(0.0f, ToYaw, 0.0f));
		FVector ActorForwardVector = SpawnedFloor->GetActorForwardVector();
		AddonVector = ActorForwardVector * 1250.0f;
	}
	
	LatestFloor = SpawnedFloor;
}


#2 框架及注意事项

UE4入门序列03(C++制作一个跑酷游戏 part 3/3)_第2张图片
游戏功能简单,逻辑代码主要在GameMode和Character里面完成了;
UE4入门序列03(C++制作一个跑酷游戏 part 3/3)_第3张图片
一般使用Component的碰撞,静态网格体的碰撞需要关闭;
UE4入门序列03(C++制作一个跑酷游戏 part 3/3)_第4张图片


#3 欢迎下载源码阅读
源码下载


你可能感兴趣的:(Unreal,Unreal)