c++ - ue4 c++ fps教程第3.2节出错,我该怎么办?
问题描述
我已经完成了关于虚幻引擎 4 的 fps 教程的第 3.2 节。在本节中,我应该通过 fire 函数实现对代码的射击,以便我可以在游戏中射击射弹。但是,在输入教程提供的代码后,我在编译时遇到了一个错误。错误如下
/Users/apple/Documents/Unreal Projects/FPSProject/Source/FPSProject/FPSCharacter.cpp:120:38:“Instigator”是“AActor”的私有成员
当我删除这一行并编译代码时,它会成功,但是当我在虚幻上测试它时,我根本无法发射弹丸。
这是代码
FPSCharacter.cpp
include "FPSCharacter.h"
include "FPSProject.h" #include "FPSCharacter.generated.h" #include "FPSProjectile.h" #include "GameFramework/Actor.h"
// Sets default values AFPSCharacter::AFPSCharacter() { // Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it. PrimaryActorTick.bCanEverTick = true;
// Create a first person camera component.
FPSCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
// Attach the camera component to our capsule component.
FPSCameraComponent->SetupAttachment(GetCapsuleComponent());
// Position the camera slightly above the eyes.
FPSCameraComponent->SetRelativeLocation(FVector(0.0f, 0.0f, 50.0f + BaseEyeHeight));
// Allow the pawn to control camera rotation.
FPSCameraComponent->bUsePawnControlRotation = true;
// Create a first person mesh component for the owning player.
FPSMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("FirstPersonMesh"));
// Only the owning player sees this mesh.
FPSMesh->SetOnlyOwnerSee(true);
// Attach the FPS mesh to the FPS camera.
FPSMesh->SetupAttachment(FPSCameraComponent);
// Disable some environmental shadowing to preserve the illusion of having a single mesh.
FPSMesh->bCastDynamicShadow = false;
FPSMesh->CastShadow = false;
// The owning player doesn't see the regular (third-person) body mesh.
GetMesh()->SetOwnerNoSee(true);
}
// Called when the game starts or when spawned void AFPSCharacter::BeginPlay() { Super::BeginPlay();
if (GEngine)
{
// Put up a debug message for five seconds. The -1 "Key" value (first argument) indicates that we will never need to update or refresh this message.
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("We are using FPSCharacter."));
}
}
// Called every frame void AFPSCharacter::Tick( float DeltaTime ) { Super::Tick( DeltaTime );
}
// Called to bind functionality to input void AFPSCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) { Super::SetupPlayerInputComponent(PlayerInputComponent);
// Set up "movement" bindings.
PlayerInputComponent->BindAxis("MoveForward", this, &AFPSCharacter::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &AFPSCharacter::MoveRight);
// Set up "look" bindings.
PlayerInputComponent->BindAxis("Turn", this, &AFPSCharacter::AddControllerYawInput);
PlayerInputComponent->BindAxis("LookUp", this, &AFPSCharacter::AddControllerPitchInput);
// Set up "action" bindings.
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AFPSCharacter::StartJump);
PlayerInputComponent->BindAction("Jump", IE_Released, this, &AFPSCharacter::StopJump);
PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &AFPSCharacter::Fire);
}
void AFPSCharacter::MoveForward(float Value) { // Find out which way is "forward" and record that the player wants to move that way. FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::X); AddMovementInput(Direction, Value); }
void AFPSCharacter::MoveRight(float Value) { // Find out which way is "right" and record that the player wants to move that way. FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::Y); AddMovementInput(Direction, Value); }
void AFPSCharacter::StartJump() { bPressedJump = true; }
void AFPSCharacter::StopJump() { bPressedJump = false; }
void AFPSCharacter::Fire() { // Attempt to fire a projectile. if (ProjectileClass) { // Get the camera transform. FVector CameraLocation; FRotator CameraRotation; GetActorEyesViewPoint(CameraLocation, CameraRotation);
// Transform MuzzleOffset from camera space to world space.
FVector MuzzleLocation = CameraLocation + FTransform(CameraRotation).TransformVector(MuzzleOffset);
FRotator MuzzleRotation = CameraRotation;
// Skew the aim to be slightly upwards.
MuzzleRotation.Pitch += 10.0f;
UWorld* World = GetWorld();
if (World)
{
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = Instigator;
// Spawn the projectile at the muzzle.
AFPSProjectile* Projectile = World->SpawnActor<AFPSProjectile>(ProjectileClass, MuzzleLocation, MuzzleRotation, SpawnParams);
if (Projectile)
{
// Set the projectile's initial trajectory.
FVector LaunchDirection = MuzzleRotation.Vector();
Projectile->FireInDirection(LaunchDirection);
}
}
}
}
解决方案
推荐阅读
- python - 在 sympy 中定义符号的函数和其他函数
- python - 在 Python 中从外部文件中读取变量值
- odoo-13 - 如何在权限正确但出现错误时验证 Odoo 13 记录?
- bash - 如何在 shell 脚本中取消 curl 请求
- node.js - AWS CodeBuild 上的 NodeSource SSL 问题
- java - 迭代器接受字符串,但我需要在偶数索引处获取元素?
- arrays - 如何将不可打印的字符转换为 C 中的十六进制值?
- html - 有没有办法在不按下其余内容的情况下将下拉菜单添加到我的导航栏的一部分?
- swift - 使用 .focused() 点击不同视图时如何移除 TextField 焦点?
- php - 如何使用 PHP preg_split 函数