Sometimes you need to check if actor is inside light or shadow. This can be usefull in stealth games or games where AI need to know how actors are influenced by lighting.
Lighting Influence is part of Game Dev Tools Plugin.
Features
- Supports Point, Spot, Directional and Rectangle lights influence,
- Directional light have own influence multiplier,
- Actor can track its lighting influence,
- Supports UE4 perception system: AI can use perception to catch see actors in light,
- EQS for searching spots in light or in shadow,
Documentation
Example Maps
Plugins\GDMegaPack\Content\LightingInfluence\Maps\Showcase_LightingInfluence.umap – map to check how actors are influenced by lighting,
Plugins\GDMegaPack\Content\LightingInfluence\Maps\Showcase_LightingInfluenceAIPerception.umap – map to check how to configure AI to see Pawns in light using perception system, BT and EQS.
Lighting Influence Configuration
Your map need to have Lighting Influence Manager.
Manager keep track of each lights in map. You can change:
- Update Interval
- Directional Ammount To Add
- Draw Debug Traces
Next your actors (which you want to keep track of) need to have Lighting Influence Receiver Component.
Component have two events InLight and InShadow. You can change trigger thresholds.
AI Perception Configuration
You can see example in GDShowcasePawnWithReceiver.h
Your Pawn or Character that you want to track need to have AISightTargetInterface implemented. Unfortinately currently (4.22) you can’t implement this interface in Blueprints. You need to override CanBeSeenFrom from this interface.
Another thing is to add LightingInfluenceReceiver Component to your Pawn or Character.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
UCLASS() class GDMEGAPACKRUNTIME_API AGDShowcasePawnWithReceiver : public APawn, public IAISightTargetInterface { GENERATED_BODY() public: // Sets default values for this pawn's properties AGDShowcasePawnWithReceiver(const FObjectInitializer& ObjectInitializer = FObjectInitializer::Get()); /* From IAISightTargetInterface to add light infuence to checks */ virtual bool CanBeSeenFrom(const FVector& ObserverLocation, FVector& OutSeenLocation, int32& NumberOfLoSChecksPerformed, float& OutSightStrength, const AActor* IgnoreActor = NULL) const; protected: // Called when the game starts or when spawned virtual void BeginPlay() override; public: // Called every frame virtual void Tick(float DeltaTime) override; // Called to bind functionality to input virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; private: UPROPERTY(Category = Components, VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) UGDLightingInfluenceReceiver* LightInfluenceReceiver; UPROPERTY() USceneComponent* SceneComponent; }; |
Now in CanBeSeenFrom implementation just use code to check lighting influence from receiver component.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
bool AGDShowcasePawnWithReceiver::CanBeSeenFrom(const FVector& ObserverLocation, FVector& OutSeenLocation, int32& NumberOfLoSChecksPerformed, float& OutSightStrength, const AActor* IgnoreActor /*= NULL*/) const { /* before check let's check if actor is outside light */ if (LightInfluenceReceiver->GetCurrentLightingInfluence() <= 0.f) { OutSightStrength = 0; return false; } static const FName NAME_AILineOfSight = FName(TEXT("TestPawnLineOfSight")); FHitResult HitResult; const bool bHit = GetWorld()->LineTraceSingleByObjectType(HitResult, ObserverLocation, GetActorLocation(), FCollisionObjectQueryParams(ECC_WorldStatic), FCollisionQueryParams(NAME_AILineOfSight, true, IgnoreActor)); NumberOfLoSChecksPerformed++; // Add any other checks you want to perform here // ... if (bHit == false || (HitResult.Actor.IsValid() && HitResult.Actor->IsOwnedBy(this))) { OutSeenLocation = GetActorLocation(); OutSightStrength = 1.f; return true; } OutSightStrength = 0; return false; } |
Your AIController need to have AIPerception Component added. You can check example in BP_ShowcaseAIControllerWithLightInfluence.
You need to keep track of visible actors using OnTargetPerceptionUpdated function.
Then in some update function inform Behavior Tree about results.
Behavior Tree is straightforward just to show example.