Advertisement
BeneFager

Traceforward

Jan 11th, 2022
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. void URInteractComponent::TraceForward(FVector Origin, FVector Direction)
  2. {
  3.     FHitResult Hit;
  4.     //GameTraceChannel1 is a custom interact trace collision
  5.     bool bHit = GetWorld()->LineTraceSingleByChannel(Hit, Origin, Origin + Direction * InteractRange,
  6.                                                      ECC_GameTraceChannel1,
  7.                                                      FColParam);
  8.     if (bHit)
  9.     {
  10.         AActor* Interactable = Hit.GetActor();
  11.  
  12.         if (Interactable)
  13.         {
  14.             if (Interactable != FocusedActor)
  15.             {
  16.                 if (FocusedActor) //Clear previous focused actor
  17.                 {
  18.                     IInteractInterface* Interface = Cast<IInteractInterface>(FocusedActor);
  19.                     if (Interface)
  20.                     {
  21.                         Interface->Execute_OnFocusLost(FocusedActor);
  22.                     }
  23.                 }
  24.                 IInteractInterface* Interface = Cast<IInteractInterface>(Interactable);
  25.                 if (Interface) //focus on new actor
  26.                 {
  27.                     Interface->Execute_OnFocus(Interactable);
  28.                 }
  29.             }
  30.             FocusedActor = Interactable;
  31.         }
  32.     }
  33.     else
  34.     {
  35.         if (FocusedActor) //Remove focused actor if nothing interactable is hit
  36.         {
  37.             IInteractInterface* Interface = Cast<IInteractInterface>(FocusedActor);
  38.             if (Interface)
  39.             {
  40.                 Interface->Execute_OnFocusLost(FocusedActor);
  41.             }
  42.         }
  43.         FocusedActor = nullptr;
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement