-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChangeMaterial.cpp
More file actions
64 lines (47 loc) · 2.12 KB
/
Copy pathChangeMaterial.cpp
File metadata and controls
64 lines (47 loc) · 2.12 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Fill out your copyright notice in the Description page of Project Settings.
#include "ChangeMaterial.h"
#include "DrawDebugHelpers.h"
#include "Components/BoxComponent.h"
// Sets default values
AChangeMaterial::AChangeMaterial()
{
// 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;
MyMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("My Mesh"));
MyMesh->SetupAttachment(RootComponent);
RootComponent = MyMesh;
MyBoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("My BoxComponent"));
MyBoxComponent->InitBoxExtent(FVector(100, 100, 100));
MyBoxComponent->SetCollisionProfileName("Trigger");
MyBoxComponent->SetupAttachment(RootComponent);
OnMaterial = CreateDefaultSubobject<UMaterial>(TEXT("OnMaterial"));
OffMaterial = CreateDefaultSubobject<UMaterial>(TEXT("OffMaterial"));
MyBoxComponent->OnComponentBeginOverlap.AddDynamic(this, &AChangeMaterial::OnOverlapBegin);
}
// Called when the game starts or when spawned
void AChangeMaterial::BeginPlay()
{
Super::BeginPlay();
DrawDebugBox(GetWorld(), GetActorLocation(), FVector(100, 100, 100), FColor::White, true, -1, 0, 10);
// Set the mesh for the material - begin with 'OffMaterial'
MyMesh->SetMaterial(0, OffMaterial);
}
// Called every frame
void AChangeMaterial::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AChangeMaterial::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if ((OtherActor != nullptr) && (OtherActor != this) && (OtherComp != nullptr))
{
MyMesh->SetMaterial(0, OnMaterial);
}
}
void AChangeMaterial::OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if ((OtherActor != nullptr) && (OtherActor != this) && (OtherComp != nullptr))
{
MyMesh->SetMaterial(1, OffMaterial);
}
}