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"

How to get the transform of the camera for line trace

I'm trying to get the start position of my line trace to be attached to the player camera. I have code setup that gets the rotation of the line cast, but its start position is relative to world coordinates rather than the camera's.

 FTransform CameraTransform = PlayerCamera->GetComponentTransform();
 // The start position of the line trace
 FVector Start = CameraTransform.TransformVector(FVector(0.0f, 0.0f, 100.0f));
 // The end position of the line trace
 FVector End = CameraTransform.TransformVector(FVector(1000.0f, 0.0f, 100.0f));
 FCollisionQueryParams RV_TraceParams = FCollisionQueryParams(FName(TEXT("Trace")), true, this);
 RV_TraceParams.bTraceComplex = true;
 RV_TraceParams.bReturnPhysicalMaterial = false;

 FHitResult RV_Hit(ForceInit);
 GetWorld()->LineTraceSingleByChannel(RV_Hit, Start, End, ECC_WorldStatic, RV_TraceParams);

My assumption is that PlayerCamera->GetComponentTransform() is the problem, but I don't know why or how. I've also tried PlayerCamera->GetSocketTransform(TEXT("PlayerCamera")), and the result was the same.

Here is a GIF of what's going on. As you can see the Start position is at world space. How do I make it local space to the camera? https://gyazo.com/39ad98b5bbfc7a1fcdda012cbf226e45

Thanks in advance to anyone who can help.

Product Version: UE 4.19
more ▼

asked May 02 '18 at 07:23 PM in C++ Programming

avatar image

11 7 5

(comments are locked)
10|2000 characters needed characters left

2 answers: sort voted first

I found the Answer.

 FVector Start = CameraTransform.GetLocation();

The trace now starts at the camera. Start did need to be added to the end variable. So thanks to xSheim as I wouldn't have know to do that.

more ▼

answered May 02 '18 at 07:50 PM

avatar image

11 7 5

(comments are locked)
10|2000 characters needed characters left

FVector End = Start + CameraTransform.TransformVector(FVector(1000.0f, 0.0f, 100.0f));
This should do the trick

more ▼

answered May 02 '18 at 07:27 PM

avatar image

125 5 11 18

avatar image NovaPixel May 02 '18 at 07:37 PM

Unfortunately this didn't work, the start is still attached to the world transform.

avatar image xSheim May 02 '18 at 07:49 PM

I'm sorry, I didn't understand the question. If your Camera is a component of the pawn, you could just get world location of it. Otherwise you should do this stuff (easier to show in blueprint, but should look nearly the same in C++): Code Result

answ2.png (75.2 kB)
answ1.png (832.1 kB)
(comments are locked)
10|2000 characters needed characters left
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