Skip to content
Snippets Groups Projects
Commit 238b85b6 authored by Marcel Krüger's avatar Marcel Krüger
Browse files

Add functionality to have enter and leave events on targatable actor

parent c6059178
Branches
No related tags found
No related merge requests found
Pipeline #167506 failed
......@@ -104,9 +104,42 @@ void UBasicVRInteractionComponent::TickComponent(float DeltaTime, ELevelTick Tic
const FTwoVectors StartEnd = GetHandRay(MaxClickDistance);
TOptional<FHitResult> Hit = RaytraceForFirstHit(StartEnd);
if (!Hit.IsSet())
{
// Execute leave event on the actor that lost the focus if there was one
if (LastActorHit && LastActorHit->Implements<UTargetable>())
{
ITargetable::Execute_OnTargetedLeave(LastActorHit);
}
if(InteractionRayVisibility==EInteractionRayVisibility::VisibleOnHoverOnly)
{
InteractionRay->SetVisibility(false);
}
LastActorHit = nullptr;
return;
}
AActor* HitActor = Hit->GetActor();
LastActorHit = HitActor; // Store the actor that was hit to have access to it in the next frame as well
// Execute Leave and enter events when the focused actor changed
if (HitActor != LastActorHit)
{
//We can always execute the enter event as we are sure that a hit occured
if (HitActor->Implements<UTargetable>())
{
ITargetable::Execute_OnTargetedEnter(HitActor);
}
//Only execute the Leave Event if there was an actor that was focused previously
if (LastActorHit != nullptr && LastActorHit->Implements<UTargetable>())
{
ITargetable::Execute_OnTargetedLeave(LastActorHit);
}
}
// for now uses the same distance as clicking
if (HitActor->Implements<UTargetable>() && Hit->Distance < MaxClickDistance)
......
......@@ -22,4 +22,12 @@ public:
// function that will be called when clickable actor got clicked, and passed the world pos of the click
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = Gameplay)
void OnTargeted(FVector WorldPositionOfTarget);
//function that will be called when a targetable actor gets focused
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = Gameplay)
void OnTargetedEnter();
//function that will be called when a targetable actor loses focused
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = Gameplay)
void OnTargetedLeave();
};
......@@ -40,7 +40,9 @@ private:
UPROPERTY() UPrimitiveComponent* ComponentSimulatingPhysics = nullptr;
UPROPERTY() UGrabbingBehaviorComponent* Behavior = nullptr;
UPROPERTY() USceneComponent* InteractionRayEmitter = nullptr;
UPROPERTY() UStaticMeshComponent* InteractionRay = nullptr;
/* Stores the reference of the Actor that was hit in the last frame*/
UPROPERTY() AActor* LastActorHit = nullptr;
void HandlePhysicsAndAttachActor(AActor* HitActor);
FTwoVectors GetHandRay(float Length) const;
TOptional<FHitResult> RaytraceForFirstHit(const FTwoVectors& Ray) const;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment