简单c++构建第一人称

本文内容为UE4.27的文档教程

GameMode确定

新建的项目会自动生成GameMode,如果有更改,而不是使用默认的GameMode类,就需要在引擎的设置中更改
image

角色的实现

前后左右移动#

//前后 MoveForward
void AFPSCharacter::MoveForward(float value)
{
	//利用控制器方向寻找前进的方向就是角色朝向
	FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::X);
	AddMovementInput(Direction, value);
}
//左右MoveRight
void AFPSCharacter::MoveRight(float value)
{
	//
	FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::Y);
	AddMovementInput(Direction, value);
}

利用AddMovementInput函数传入方向

在模板自带的PlayerInputComponent方法下,绑定行为函数

void AFPSCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	PlayerInputComponent->BindAxis("MoveForward", this, &AFPSCharacter::MoveForward);
	PlayerInputComponent->BindAxis("MoveRight", this, &AFPSCharacter::MoveRight);
}

绑定轴的移动:BindAxis

鼠标控制视角的转动#

PlayerInputComponent->BindAxis("Turn", this, &AFPSCharacter::AddControllerYawInput);
PlayerInputComponent->BindAxis("LookUp", this, &AFPSCharacter::AddControllerPitchInput);

AddControllerYawInput,AddControllerPitchInput为基类自带可以直接使用来控制视角的转动

跳跃#

跳跃与bPressedJump绑定这是模板基类设置好的。所以在Character的源文件和头文件中定义两个函数来控制这个变量值

void AFPSCharacter::StartJump()
{
	bPressedJump = true;
}

void AFPSCharacter::StopJump()
{
	bPressedJump = false;
}

在模板自带的PlayerInputComponent方法下,绑定行为函数

PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AFPSCharacter::StartJump);
PlayerInputComponent->BindAction("Jump", IE_Released, this, &AFPSCharacter::StopJump);

绑定按键操作:BindAction,IE_Pressed表示设置的按键按下,IE_Released表示设置的按键松开

实现第一人称视角#

FPS游戏常见的方法就是使用两个分开的角色网格体,一个是全身网格,一个是“武器和手部”的局部网格,全身网格可以用于第三人称视角,局部网格用于第一人称视角。
主要操作,摄像机组件Attach在全身网格下,局部网格Attach在摄像机下。禁用某些阴影更加真实(防止第一个网格体的影响)

FPSMesh->bCastDynamicShadow = false;
FPSMesh->CastShadow = false;

发射物的实现

设置一个球状发射物利用USphereComponent,利用UProjectileMovementComponent确定发射物的移动,定义一个函数用于管理发射物的发射

//传入发射方向,在角色类中获取,乘以发射速度
void AFPSProjectile::FireInDirection(const FVector& ShootDirection)
{
    ProjectileMovementComponent->Velocity = ShootDirection * ProjectileMovementComponent->InitialSpeed;
}

在角色类中利用BindAction绑定按键,并且编写Fire函数,在角色类中需要新增一个变量用来确定角色需要发射哪一类发射物虽然现在只有1类。
BindAction需要一个角色类中的操作(函数),所以需要在角色类中再写一个Fire函数,其中调用发射物类中的发射函数

//在头文件中
UPROPERTY(EditDefaultsOnly, Category = Projectile)
TSubclassOf<class AFPSProjectile> ProjectileClass;

实现射击#

在角色类的头文件中include发射物头文件,编写Fire函数,利用SpawnActor方法来生成发射物,调用发射物类中的发射函数实现发射。

void AFPSCharacter::Fire()
{
// 试图发射发射物。
if (ProjectileClass)
{
// 获取摄像机变换。
FVector CameraLocation;
FRotator CameraRotation;
GetActorEyesViewPoint(CameraLocation, CameraRotation);

// 设置MuzzleOffset,在略靠近摄像机前生成发射物。
MuzzleOffset.Set(100.0f, 0.0f, 0.0f);

// 将MuzzleOffset从摄像机空间变换到世界空间。
FVector MuzzleLocation = CameraLocation + FTransform(CameraRotation).TransformVector(MuzzleOffset);

// 使目标方向略向上倾斜。
FRotator MuzzleRotation = CameraRotation;
MuzzleRotation.Pitch += 10.0f;

UWorld* World = GetWorld();
if (World)
{
    FActorSpawnParameters SpawnParams;
    SpawnParams.Owner = this;
    SpawnParams.Instigator = GetInstigator();

    // 在枪口位置生成发射物。
    AFPSProjectile* Projectile = World->SpawnActor<AFPSProjectile>(ProjectileClass, MuzzleLocation, MuzzleRotation, SpawnParams);
    if (Projectile)
    {
        // 设置发射物的初始轨迹。
        FVector LaunchDirection = MuzzleRotation.Vector();
        Projectile->FireInDirection(LaunchDirection);
    }
}
}
}

设置发射物的生命和碰撞#

在发射物类的源文件中的构造函数中利用InitialLifeSpan设置生命周期
在引擎设置Collision中自定义Collision文件,在源文件构造函数中对SphereComp绑定该文件

SphereComp->BodyInstance.SetCollisionProfileName(TEXT("Projectile"));

当发射物碰撞物体消失#

利用OnComponentHit事件,在发射物类中

SphereComponet->OnComponentHit.AddDynamic(this, &AFPSProjectile::OnHit);

void AFPSProjectile::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit)
{
    if (OtherActor != this && OtherComponent->IsSimulatingPhysics())
    {
        OtherComponent->AddImpulseAtLocation(ProjectileMovementComponent->Velocity * 100.0f, Hit.ImpactPoint);
    }

    Destroy();
}

作者:XTG111

出处:https://www.cnblogs.com/XTG111/p/17795970.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   XTG111  阅读(119)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
more_horiz
keyboard_arrow_up light_mode palette
选择主题
menu
点击右上角即可分享
微信分享提示