SimpleShooter Zoom in

Hi!
I recently finished the Simple Shooter course and wanted to extend on it a little bit. I basically want a zoom on RightMouse click.


I’ve made this in about a minute in BP and it works exactly the way I want it too. However, I’d like to replicate this in C++ since every thing else is there as well and I figured this would be simple enough for me to learn how to do this in C++ (spoiler, I was wrong).

I made two bind actions:

PlayerInputComponent->BindAction(TEXT(“Zoom”), EInputEvent::IE_Pressed, this, &AShooterCharacter::ZoomIn);
PlayerInputComponent->BindAction(TEXT(“Zoom”), EInputEvent::IE_Released, this, &AShooterCharacter::ZoomOut);

I then made two functions, ZoomIn, and ZoomOut:
(The debug log was there to test the IE_Released, all working as expected).

void AShooterCharacter::ZoomIn()

{

// On Pressed. SetFieldOfView() to 90;

UE_LOG(LogTemp, Warning, TEXT("Zoom in"));

}

void AShooterCharacter::ZoomOut()

{

// On Released. SetFieldOfView() to 90;

UE_LOG(LogTemp, Warning, TEXT("Zoom out"));

}

I found a function for SetFieldOfView() but I have no idea how to simply get the player’s camera, or how to implement the function. Two hours in I just wish it was as simple as the Blueprint implementation :wink:

Any suggestions where to look, or what I’m missing?

Thanks for taking a look!

If you have created the whole actor in C++ you should have something like this creating the camera in the class constructor. you can reference the FOV via that (after checking its not null)

	CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComp"));
	CameraComp->SetupAttachment(SpringArmComp);

so something like

CameraComp->SetFieldOfView(NewFOV)

to read the FOV it’s just

DefaultFOV = CameraComp->FieldOfView;

if you have a basic c++ actor class and you have added the camera components in blueprint, I think this should work if you run this on begin play not the constructor (I’ve not tested it with camera but have done it with other components so can’t see why it wouldn’t work) It will return the first camera it finds, if you have multiple cameras you’ll have to research how to get an array back or something like that, I’m sure there must be a way.

It might need a bit of tweaking but hopefully should get you going!

CameraComp = FindComponentByClass<UCameraComponent>();

then same as first example to reference it after checking if it’s null.

Make sure you close UE editor when you compile this and then reopen it or you’ll get weird issues with the hot reload.

Thanks! Initially it was setup manually in blueprint, , but I replaced setting up the camera/springarm components in c++ and used the first method you suggested.

Thanks a bunch!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms