我正在尝试发出从相机位置开始到一定距离的光线。这是我制造的“枪”。当我尝试访问除构造器之外的其他任何地方的摄像头时,我得到的摄像头为空。
当我运行调试器时,摄像机在到达第一个“ BeginPlay”功能之前返回null。 CameraSpringArm
仍然附着在对象上。
我尝试重新整理头文件中的函数和指针,但仍然卡住了。
cpp:
#include "MyCharacter.h"
// Sets default values
AMyCharacter::AMyCharacter()
{
ThirdPerson = 2;
FirstPerson = 1;
// 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.
CameraSpringArm = CreateDefaultSubobject<USpringArmComponent>
(TEXT("CameraSpringArm"));
CharacterCamera =
CreateDefaultSubobject<UCameraComponent(TEXT("FirstPersonCamera"));
// Attach the camera component to our capsule component.
CameraSpringArm->SetupAttachment(RootComponent);
CharacterCamera->SetupAttachment(CameraSpringArm);
// Allow the pawn to control camera rotation.
CharacterCamera->bUsePawnControlRotation = true;
SwitchCamera(CharacterCamera, CameraSpringArm, ThirdPerson);
bPressedCameraSwap = false;
bFire = false;
}
// Called to bind functionality to input
void AMyCharacter::SetupPlayerInputComponent(UInputComponent*
PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveForward", this,
&AMyCharacter::MoveForward);
PlayerInputComponent->BindAxis("MoveSide", this, &AMyCharacter::MoveSide);
PlayerInputComponent->BindAxis("TurnUp", this,
&AMyCharacter::AddControllerYawInput);
PlayerInputComponent->BindAxis("TurnSide", this,
&AMyCharacter::AddControllerPitchInput);
PlayerInputComponent->BindAction("CameraSwap", IE_Pressed, this,
&AMyCharacter::CameraSwap);
PlayerInputComponent->BindAction("CameraSwap", IE_Released, this,
&AMyCharacter::CameraSwapStop);
PlayerInputComponent->BindAction("Fire", IE_Pressed, this,
&AMyCharacter::Fire);
}
void AMyCharacter::Fire()
{
bFire = true;
float Distance = 1000.0f;
FHitResult HitResult;
FVector StartTrace = CharacterCamera->GetComponentLocation(); //Its
breaking here
FVector ForwardVector = CharacterCamera->GetForwardVector();
FVector EndTrace = ((ForwardVector * Distance) + StartTrace);
FCollisionQueryParams TraceParams;
GetWorld()->LineTraceSingleByChannel(HitResult, StartTrace, EndTrace,
ECC_Visibility, TraceParams);
DrawDebugLine(GetWorld(), StartTrace, EndTrace,FColor(255,0,0), true);
}
标题
#pragma once
#include "CoreMinimal.h"
#include "EngineGlobals.h"
#include "Engine/Engine.h"
#include "Components/InputComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/Controller.h"
#include "GameFramework/Character.h"
#include "GameFramework/SpringArmComponent.h"
#include "DrawDebugHelpers.h"
#include "MyCharacter.generated.h"
UCLASS()
class LEARNING_API AMyCharacter : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
AMyCharacter();
protected:
// Called when the game starts or when spawned
void BeginPlay() override;
public:
// Called every frame
void Tick(float DeltaTime) override;
// Called to bind functionality to input
void SetupPlayerInputComponent(class UInputComponent*
PlayerInputComponent) override;
UPROPERTY(VisibleAnywhere)
UCameraComponent* CharacterCamera;
UPROPERTY(VisibleAnywhere)
USpringArmComponent* CameraSpringArm;
UPROPERTY()
bool bPressedCameraSwap;
UPROPERTY()
bool bFirstPersonEnabled{};
UPROPERTY()
bool bThirdPersonEnabled{};
UPROPERTY()
bool bFire;
protected:
UFUNCTION()
void MoveForward(float Val);
//handles strafing
UFUNCTION()
void MoveSide(float Val);
//Handles swaping camera on press
UFUNCTION()
void CameraSwap();
//Clears swaping camera Flag
UFUNCTION()
void CameraSwapStop();
UFUNCTION()
void Fire();
UFUNCTION()
void FireStop();
答案 0 :(得分:0)
您的CharacterCamera
是一个指针,而不是实际的对象。因此,在构造函数内部,您将AMyCharacter.CharacterCamera
设置为临时对象的地址,并且当构造函数的作用域结束时,该临时对象将被解构且指针变为无效。
有几种解决方法,一种是将CharacterCamera
更改为实际的UCameraComponent
,而不仅仅是一个指针。
另一种解决方案是在函数外部实例化UCameraComponent
,以免在构造函数的末尾对其进行解构。
答案 1 :(得分:0)
答案是虚幻引擎中存在一个非常讨厌的错误。我必须更改CharacterCamera变量的名称。我认为这是因为我在“ CreateDefaultSubobject”函数中编辑了字符串。