Add ToggleLight class to trigger lights in radius
+ Followed UE4 tutoial on mixing C++ and Blueprints + https://docs.unrealengine.com/4.27/en-US/ProgrammingAndScripting/ClassCreation/CodeAndBlueprints/ + Added an in-editor and blueprint field ToggleLight::Radius to adjust trigger radius + Made stairs light up as you walk up them
This commit is contained in:
59
Source/ThirdPerson/Private/ToggleLight.cpp
Normal file
59
Source/ThirdPerson/Private/ToggleLight.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
// All content (c) Shaun Reed 2021, all rights reserved
|
||||
|
||||
|
||||
#include "ToggleLight.h"
|
||||
|
||||
#include "Components/PointLightComponent.h"
|
||||
#include "Components/SphereComponent.h"
|
||||
|
||||
// Sets default values
|
||||
AToggleLight::AToggleLight()
|
||||
{
|
||||
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
|
||||
PrimaryActorTick.bCanEverTick = true;
|
||||
|
||||
DesiredIntensity = 3000.0f;
|
||||
|
||||
PointLight1 = CreateDefaultSubobject<UPointLightComponent>(TEXT("PointLight1"));
|
||||
PointLight1->Intensity = DesiredIntensity;
|
||||
// PointLight1->bVisible = true;
|
||||
RootComponent = PointLight1;
|
||||
|
||||
Sphere1 = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere1"));
|
||||
Sphere1->InitSphereRadius(Radius);
|
||||
Sphere1->SetupAttachment(RootComponent);
|
||||
|
||||
Sphere1->OnComponentBeginOverlap.AddDynamic(this, &AToggleLight::OnOverlapBegin); // set up a notification for when this component overlaps something
|
||||
Sphere1->OnComponentEndOverlap.AddDynamic(this, &AToggleLight::OnOverlapEnd); // set up a notification for when this component overlaps something
|
||||
}
|
||||
|
||||
void AToggleLight::OnOverlapBegin_Implementation(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
|
||||
{
|
||||
if (OtherActor && (OtherActor != this) && OtherComp)
|
||||
{
|
||||
ToggleLight();
|
||||
}
|
||||
}
|
||||
|
||||
void AToggleLight::OnOverlapEnd_Implementation(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
|
||||
{
|
||||
if (OtherActor && (OtherActor != this) && OtherComp)
|
||||
{
|
||||
ToggleLight();
|
||||
}
|
||||
}
|
||||
|
||||
void AToggleLight::ToggleLight()
|
||||
{
|
||||
PointLight1->ToggleVisibility();
|
||||
}
|
||||
|
||||
void AToggleLight::Tick(float DeltaTime)
|
||||
{
|
||||
if (Radius != Sphere1->GetUnscaledSphereRadius())
|
||||
{
|
||||
Sphere1->SetSphereRadius(Radius);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user