Hi I am trying to change the color of this floating box when it is hit by a projectile. I am not sure what the code is to change the material of an aactor in C++. This is my current code
// Fill out your copyright notice in the Description page of Project Settings.
#include "ProjectileTrigger.h"
// Sets default values for this component's properties
UProjectileTrigger::UProjectileTrigger()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
BoxComp = CreateDefaultSubobject<UBoxComponent>(TEXT("RootCollision"));
MeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComp"));
OnHitMesh = CreateDefaultSubobject<UMaterial>(TEXT("OnHitMaterial"));
BoxComp->SetBoxExtent(FVector(75, 75, 50));
BoxComp->OnComponentHit.AddDynamic(this, &UProjectileTrigger::DetectHit);
NewMovement = FVector(50, 0, 0);
}
// Called when the game starts
void UProjectileTrigger::BeginPlay()
{
Super::BeginPlay();
// ...
}
// Called every frame
void UProjectileTrigger::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
AActor* Parent = GetOwner();
if (Parent)
{
Parent->SetActorLocation(Parent->GetActorLocation() + NewMovement * DeltaTime);
}
}
void UProjectileTrigger::DetectHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
AActor* Parent = GetOwner();
}
and my .h is here
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include <Components/BoxComponent.h>
#include "ProjectileTrigger.generated.h"
class UBoxComponent;
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class FPSGAME_API UProjectileTrigger : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UProjectileTrigger();
protected:
// Called when the game starts
virtual void BeginPlay() override;
UPROPERTY(VisibleAnywhere, Category = "CollisionComp")
UBoxComponent* BoxComp;
UPROPERTY(VisibleAnywhere, Category = "CollisionComp")
UStaticMeshComponent* MeshComp;
UPROPERTY(EditAnywhere, Category = "MaterialonHit")
UMaterial* OnHitMesh;
UPROPERTY(EditInstanceOnly, Category = "Movement")
FVector NewMovement;
UFUNCTION()
void DetectHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
};