Skip to content
Snippets Groups Projects
Commit 9b1b7c02 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
1 merge request!44Add enter and leave events for targetable interface
Pipeline #167514 failed
......@@ -104,15 +104,44 @@ 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);
}
LastActorHit = nullptr;
return;
}
AActor* HitActor = Hit->GetActor();
// 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)
{
ITargetable::Execute_OnTargeted(HitActor, Hit->Location);
}
LastActorHit = HitActor; // Store the actor that was hit to have access to it in the next frame as well
}
void UBasicVRInteractionComponent::Initialize(USceneComponent* RayEmitter, float InMaxGrabDistance, float InMaxClickDistance)
......
......@@ -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,8 @@ private:
UPROPERTY() UPrimitiveComponent* ComponentSimulatingPhysics = nullptr;
UPROPERTY() UGrabbingBehaviorComponent* Behavior = nullptr;
UPROPERTY() USceneComponent* InteractionRayEmitter = 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