I am trying to just shoot a ray from the camera that the user is using. It is telling me that the camera is a null pointer.
// Fill out your copyright notice in the Description page of Project Settings.
#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 when the game starts or when spawned
void AMyCharacter::BeginPlay()
{
Super::BeginPlay();
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("We are using MyCharacter!"));
}
}
// Called every frame
void AMyCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// 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::MoveForward(float Value)
{
if (Value != 0.0f)
{
AddMovementInput(GetActorForwardVector(), Value);
}
}
void AMyCharacter::MoveSide(float Value)
{
if (Value != 0.0f)
{
AddMovementInput(GetActorRightVector(), Value);
}
}
void AMyCharacter::CameraSwap()
{
bPressedCameraSwap = true;
if (bThirdPersonEnabled)
{
SwitchCamera(CharacterCamera, CameraSpringArm, ThirdPerson);
bFirstPersonEnabled = true;
bThirdPersonEnabled = false;
}
else
{
bFirstPersonEnabled = false;
SwitchCamera(CharacterCamera, CameraSpringArm, FirstPerson);
bThirdPersonEnabled = true;
}
}
void AMyCharacter::CameraSwapStop()
{
bPressedCameraSwap = false;
}
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);
}
Here is the header file.
#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 "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;
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();
public:
//If False there is an error. If true it was successful
bool SwitchCamera(UCameraComponent* Camera, USpringArmComponent* SpringArm, int CameraType);
//Set Spring Arm properties when changing views
bool SetSpringArmProp(USpringArmComponent* SpringArm, int View);
UCameraComponent* GetCharacterCamera() const { return CharacterCamera; }
UPROPERTY(VisibleAnywhere)
UCameraComponent* CharacterCamera;
UPROPERTY(VisibleAnywhere)
USpringArmComponent* CameraSpringArm;
UPROPERTY()
bool bPressedCameraSwap;
UPROPERTY()
bool bFirstPersonEnabled;
UPROPERTY()
bool bThirdPersonEnabled;
UPROPERTY()
bool bFire;
//A camera type used for SwitchCamera
int ThirdPerson;
//A camera type used for SwitchCamera
int FirstPerson;
};