x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

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.

Product Version: Not Selected
Tags:
more ▼

asked Oct 20 '14 at 11:31 PM in C++ Programming

avatar image

ArcainOne
489 32 25 57

avatar image creasso Oct 20 '14 at 11:52 PM

You can achieve this by getting a CameraManager class that will be tied to your Controller. This way, whatever you'll be "possessing" you'll keep the same viewpoint.

avatar image ArcainOne Oct 22 '14 at 05:30 PM

Hi Creasso,

I'll look into using the CameraManager. I had miss read some information on it involving subclassing. Hopefully this will work.

avatar image ArcainOne Oct 27 '14 at 03:59 PM

I attempted to Setup the Custom Camera Manager but the concepts are a bit foreign to me. I Setup the system with three classes: CustomCharacter, CustomPlayerController, and CustomCameraManager. I then moved the camera and the camera boom onto the CustomPlayerController and set the CameraManagerClass equal to the CustomChameraManager class. At Begin Play I attached the Boom to the Root Component of the Character and overrode the function "UpdateViewTarget" to find and use the Camera attached but I am still getting the "crotch view" instead of the over the shoulder view.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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:

 AActor* ignoreActor = NULL;
 if(this->attachedPawn != NULL)
 {
     ignoreActor = this->attachedPawn;
 }
 else
 {
     ignoreActor = this->GetOwner();
 }
 FCollisionQueryParams QueryParams(TraceTagName, false, ignoreActor);

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.

more ▼

answered Oct 30 '14 at 05:07 PM

avatar image

ArcainOne
489 32 25 57

avatar image Mikhail_G Jun 05 '16 at 12:58 PM

Hello I have been trying to implement your approach, and I got stuck on this line of code if(this->attachedPawn != NULL). Where do I have to initialize attachedPawn? I have a function like this

void UArmComponent::SetAttachedActor(AActor* NewAttachedActor) { attachedPawn = NewAttachedActor; }

And and I do not call it. I do not know where and when.

avatar image ArcainOne Jun 06 '16 at 04:38 PM

Oh wow, I sort of forgot I tried this once.

However to answer your question I was calling SetAttachedPawn from the PlayerController::Possess(inPawn) and PlayerController::Unpossess()

I have found that in Replication/multiplayer managing camera systems is a bit tricky. Just an FYI.

Unfortunately I found some other issues with the above approach but have since figured out a much better way to achieve this (at least in my opinon). Even though the documentation says not to subclass the Camera Manager, I now subclass the camera manager. The camera manager keeps pointers to a SpringArm and A CameraComponent that is null at first. Then during play it checks if the camera system is initialized, and if it is not it builds the camera system dynamically. Now the camera manager is responsible for building and destroying the camera system, This also allows me to leverage the camera system for building different camera setups such as First Person and Third Person Perspectives with out spreading the code throughout the PlayerController and the Pawn. Just cleaner.

I also now realize I should update my old answewr :) thanks.

avatar image Mikhail_G Jun 06 '16 at 08:45 PM

You never know what is going to bite your in the a** :) Anyway, thank you your answer has cleared some aspects. In your previous answers you said I Setup the system with three classes: CustomCharacter, CustomPlayerController, and CustomCameraManager. I then moved the camera and the camera boom onto the CustomPlayerController a specially I then moved the camera and the camera boom onto the CustomPlayerController So CustomPlayerController did not have a camera and a boom since you had moved it to The camera manager keeps pointers to a SpringArm and A CameraComponent that is null at first or The camera manager keeps only pointers as you said, and the controller keeps an instance of them?

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question