0
\$\begingroup\$

I am trying to learn more about raycasting in C++ and am trying to implement raycasts in a mini third-person game I am creating. In order to shoot a raycast, I first need to determine its starting location, which in this case I intend it to be from the center of the third-person camera already built-in.

In order to do that, however, I need to access the properties of the camera already in the scene and store its location in a variable using c++. I am relatively new to unreal so I have no idea how to access an object's properties that's already in the scene using C++.

Any help is appreciated

I am still searching, but I couldn't find a satisfying answer yet.

\$\endgroup\$
0
\$\begingroup\$

Getting the camera itself is fairly easy:

APlayerCameraManager *camManager = GetWorld()->GetFirstPlayerController()->PlayerCameraManager;

FVector camLocation = camManager->GetCameraLocation();
FVector camForward  = camManager->GetCameraRotation().Vector();

however getting the center of the view is better done via the HUD api and it's project/deproject functions. It can also be accessed from the player controller with following API:

Documentation

//Prototype
bool DeprojectScreenPositionToWorld
(
    float ScreenX,
    float ScreenY,
    FVector & WorldLocation,
    FVector & WorldDirection
) const

To get all objects of specific class that are already in scene you could use TActorIteratorfor example like follows:

//This will iterate over every static mesh that is placed in the world(scene).
for (TActorIterator<AStaticMeshActor> ActorItr(GetWorld()); ActorItr; ++ActorItr)
{
    // Same as with the Object Iterator, access the subclass instance with the * or -> operators.
    AStaticMeshActor *Mesh = *ActorItr;
    ClientMessage(ActorItr->GetName());
    ClientMessage(ActorItr->GetActorLocation().ToString());
}
|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.