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"

[C++] Get the camera look at vector.

Is this possible? For example, if I'm spawning an object and want it to move directly away from the camera. Is there a way to get this vector to set the objects velocity?

Product Version: Not Selected
more ▼

asked Mar 25 '14 at 06:11 PM in C++ Programming

avatar image

116 18 24 24

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

3 answers: sort voted first

You can convert a FRotator to a unit FVector using .Vector() like so:

 FVector Direction = Rotation.Vector();

This will be the direction the controller is facing, not necessarily directly away from the camera. If you wanted to make it move directly away from the camera, you would want to do this:

 // Get a player controller to make sure this is a player pawn.
 // Also make sure it has a camera manager.
 APlayerController* PC = Cast<APlayerController>(Controller);
 if ( PC && PlayerCameraManager )
     // Determine direction vector using Dir = B - A
     FVector CameraLocation = PC->PlayerCameraManager->GetCameraLocation();
     FVector MyLocation = GetActorLocation();
     FVector Direction = MyLocation - CameraLocation;
     // Normalize the direction to a unit vector.
     // ... now we can do something with the direction!

more ▼

answered Mar 25 '14 at 10:31 PM

avatar image

Bob Tellez STAFF
296 7 5 9

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


To obtain the current rotation of a UCameraComponent:

 FRotator Rotation(camComp->componentToWorld.GetRotation());


Then use the little snippet from the FPS Tutorial.

 void AFPSCharacter::MoveForward(float Value)
         if ( (Controller != NULL) && (Value != 0.0f) )
             // find out which way is forward
             FRotator Rotation = Controller->GetControlRotation();
             // Limit pitch when walking or falling
             if ( CharacterMovement->IsMovingOnGround() || CharacterMovement->IsFalling() )
                 Rotation.Pitch = 0.0f;
             // add movement in that direction
             const FVector Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::X);
             AddMovementInput(Direction, Value);

more ▼

answered Mar 25 '14 at 06:27 PM

avatar image

720 32 26 55

avatar image Skydive Mar 25 '14 at 06:31 PM

I'm not using the ACharacter class. I'm talking about getting the view vector from the UCameraComponent itself. Apologies, I should've been more specific.

avatar image DarthB Mar 25 '14 at 06:39 PM

FRotator Rotation(camComp->componentToWorld.GetRotation()); shall return you the FRotator that u can use to get the Forward Vector as described in the code above.

[EDIT]Use getter method instead of attribute[/EDIT]

avatar image Skydive Mar 25 '14 at 06:50 PM

Thank you very much!

(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