Attaching camera to Pawn at runtime
I'm trying to call a function for attaching an already created camera to the calling pawn, but I can't even find the camera in the scene. The UCameraComponent is a static member of the MyPlayerController class. This is what I do in the constructor:
And this is the function that is called by the pawn (after the pawn's BeginPlay() method is called):
I get the output "NOT NULL", so I'd assume that the camera was actually attached to the pawn. When I look in the editor, that's apparently not the case. The editor crashes if I try to run the outcommented code. What might be the problem?
edit: also the engine crashes when I try to access a UCameraComponent Pointer that was declared in the MyPawn class, instead of usind a local variable.
I think you overthinking this :p I think you not aware of fact that PlayerController can do what you trying to do, which i assuming is switching pawn possession and switching camera
First you can switch pawn attached to controller using Possess function:
Then from controller you can access possess Pawn from GetPawn() as well all PlayerController function will cooperate with that pawn (like input controls)
2nd, the camera. Camera system in UE4 is actor focus based, you set actor that will be View Target for PlayerController with this function:
When camera menager has view target set, it will ask view target (which is actor) on every frame for camera position by calling CalcCamera in view target actor:
In default code in CalcCamera (which oyu can override) it picks first camera component in actor and uses it's coordinates for camera (yes CameraComponent is not really needed for camera to work, but it's helpful).
Here you got default code from engine source code:
As you can see it all about setting OutResult which contains camera attributes.You can freely override this function, if you have 2 or more camera components in actor you need to use this function to select one of them accprding to some conditions
Also keep in mind that when you possess pawn, by default it's automatically becomes view target. You can disable this behavior by setting bAutoManageActiveCameraTarget to false in your PlayerController
answered Aug 30 '15 at 04:00 AM
Follow this question
Once you sign in you will be able to subscribe for any updates here