C++ How to clamp a camera

I’ve been reading to find a way to clamp my player’s camera pitch to -45/60 strictly in code, I’ve searched everywhere on the interwebs and I only see blueprints, it can be done in BPs but I prefer to hardcore it to my playerclass instead of fiddling around with hacks on BPs. Anyways what would be the best approach for this? I’ve read up on FMath::Clamp / FRotator Clamp(), but the documentation is not helping me at all, this is what I’ve done.

CameraComponent->SetRelativeRotation(FMath::Clamp(CameraComponent->RelativeRotation, FVector(0, -45, 0), FVector(0, 60, 0)));

Obviously it didn’t work, I tried doing it the same way it’s done on blueprints but that didn’t work either, any help would be appreciated :D.

Are you sure you’re using a CameraComponent? Out of the box, the default camera is managed using PlayerCameraManager.

Incidentally, that is where you would easily clamp camera angles:


	/** pitch limiting of camera facing direction */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=PlayerCameraManager)
	float ViewPitchMin;

	/** pitch limiting of camera facing direction */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=PlayerCameraManager)
	float ViewPitchMax;

	/** yaw limiting of camera facing direction */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=PlayerCameraManager)
	float ViewYawMin;

	/** yaw limiting of camera facing direction */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=PlayerCameraManager)
	float ViewYawMax;

	/** roll limiting of camera facing direction */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=PlayerCameraManager)
	float ViewRollMin;

	/** roll limiting of camera facing direction */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=PlayerCameraManager)
	float ViewRollMax;

Where would you put the Clamp portion of the code for the camera? I assume it needs to be somewhere that gets called every frame correct?

Use PlayerCameraManager and then you’ll be able to set up min/max angles directly from the editor(but also from C++).

I am assuming an instance has to be created in code because im not seeing it as an addable component.

It’s not a component but a standalone class.

  1. Create a PlayerCameraManager blueprint

  2. Set up desired min/max angles inside the PlayerCameraManager class
    aADMNMT.png

  3. Set up your PlayerCameraManager in your PlayerController
    hTJYVJk.png

Of course all of this can be done in C++, by creating a class that derives from APlayerCameraManager(https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Camera/APlayerCameraManager/index.html), setting up the min/max angles in the class code and then referencing it in PlayerController constructor.



//This should be inside the constructor of your PlayerController
PlayerCameraManagerClass = AMyCameraManagerClass::StaticClass();


Ok thanks for the help.

This will work with 3rd Person camera setups right.