Attaching a Camera to Pawn from Controller
In the game I am working on I want the player to be able to possess any NPC in the game. However, I'd rather not have a camera component attached to EVERY character. It clutters the scene and I'd rather the character controller attach and detach a camera component dynamically.
I tried creating the camera component on the controller and attaching it to the character when the game starts, but the view is always wrong (appearing from the groin of the character) rather than over the shoulder where it should be. I've tried overloading the Calculate View function on the Pawn (and the controller I think) with the same result. When I create the camera component on the Pawn however it picks it up just fine. I am currently using the Third Person tutorial setup with a spring arm that can zoom in and out of First Person mode.
I finally did figure this out and a partial solution can be found on my other post: Setup A Dynamic Camera Third Person Camera
The key to the solution was to create a new USpringArmComponent I called UAssignableSpringArmComponent which overrode the the UpdateDesiredSpringArm function to perform a sweeping trace but ignore a specifically assigned Pawn rather than the Owner of the spring arm. Because the USpringArmComponent was not owned by the Pawn, it was colliding with the pawn and changing the arm length because of the collision. The function was copied mostly from the documentation with the key line of the sweep changed to:
The next thing I had to do is derive a CameraManager to basically use the camera attached to the player controller instead of the pawn. I overrode the UpdateViewTarget function, changing the key segment of code as described in my answer on Setup A Dynamic Third Person Camera. Basically instead of looking for a Pawn, the UpdateViewTarget looks for the Camera on my CustomController and and if it finds it, it uses that camera, other wise it performs the default UpdateViewTarget function.
answered Oct 30 '14 at 05:07 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here