Camera clipping into character's mesh

Are you trying to allow the player to see their own body when they look down? If you don’t care about that, most games wouldn’t attach the camera directly to the player model, but rather just have it hover at a reasonable elevation for eye height.

Hi, i’m working on a fps game, the problem i have is that if i attach the camera to the head bone, the camera will shake when the animation shake the mesh’s head.
If i don’t attach the camera to the mesh it will clip into the character’s mesh when he is leaning, i tried to change the position of the camera manually, this ends up making the camera doing really strange thing. Here’s the code i used to move the camera with the head’s bone in my character’s tick function :

if(GetCurrentCameraMode() == ECameraMode::CM_FirstPerson)
			{ 
				GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Orange, TEXT("trying to cast CameraManager"));
				APlayerController* PC = Cast<APlayerController>(GetController());
				APlayerCameraManager* CameraManager = Cast<APlayerCameraManager>(PC->PlayerCameraManager);
				if (CameraManager)
				{
					GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("CameraManager Cast succeed"));
					FVector HeadLocation = GetMesh()->GetSocketLocation(FName("HeadSocket"));
					FVector CameraLocation = CameraManager->GetCameraLocation();

					if (HeadLocation != CameraLocation)
					{
						if (HeadLocation.X != CameraLocation.X)
						{
							CameraComp->AddAdditiveOffset(FTransform(FQuat(), FVector(HeadLocation.X - CameraLocation.X, 0.0f, 0.0f)), CameraComp->FieldOfView);
						}

						if (HeadLocation.Y != CameraLocation.Y)
						{
							CameraComp->AddAdditiveOffset(FTransform(FQuat(), FVector(0.0f, HeadLocation.Y - CameraLocation.Y, 0.0f)), CameraComp->FieldOfView);
						}

						if (HeadLocation.Z != CameraLocation.Z)
						{
							CameraComp->AddAdditiveOffset(FTransform(FQuat(), FVector(0.0f, 0.0f, HeadLocation.Z - CameraLocation.Z)), CameraComp->FieldOfView);
						}
					}
				}
			}

I think this is correct, maybe the camera’s not supposed to be moved like that or i am doing something wrong ?
Thank you for helping me !

Yes, but if i don’t attach the camera to the player model, when the character lean down, the camera gets behind the character, which is not what i want.

You can add an offset to the camera’s position based on the player’s view elevation to combat that issue. That’s how I’ve seen it implemented in every game that allows you to see your own body.

Could you explain this step a bit further in detail, Janimationd? How would I add that offset?