Aim snapping solution

I’m trying to make an aim snapping. When the player is looking at an interactable object (like a door or a light switch), the aim cross snap the object. If he is looking anything else, the aim cross moves to 5 meters in front of the player vision.
The code behind my game just sweeps a sphere, and if it detects an interactive object and if that interactive object is being rendered on the screen, it sets a variable called LookingActor to the interactable object that the player is looking.

As you can see in the video bellow, The aim cross snaps to the door and to the light switch. If the sweeped sphere gets the light switch behind the wall, the aim cross doesn’t snap, so it’s ok, but if the sweeped sphere gets a light switch behind the window (or any other translucent material) the aim cross snaps to it, and I don’t want this to happen.

Do someone have an idea about how can I solve this problem or know another way to make this system?

Where is the start and end location of your sphere trace? Is it just a sphere around the crosshair or do you sweep from the character to the crosshair?
Regardless of that, a solution would be to do another (line) trace from the detected interactable object to the character. If that trace has a blocking hit there is obviously something between the character and the object and the crosshair should not snap.



FHitResult Hit;
if (GetWorld()->SphereTraceSingleForObjects(this, EyeLocation, TraceEnd, 10.0f, NULL, true, this, /*PICK ONE*/, Hit, true)
{
    // Blocking hit
    AActor* HitActor = Hit.GetActor();
}


Something like that, check if the HitActor is something to snap to, if it is go ahead. Also make sure you are using Single and not Multi. Multi will store a bunch of things it hit. Single will only store the first one. Multi would say, I hit a wall and a switch. Single would just detect the wall.

Good luck!