Hello!
So I am trying to implement a mechanism where player can pick up items in the world (coffee cup, book, chair, and etc) and use it to deal damage (swing or throw) only when it is picked up by someone and cannot deal damage to whoever is holding it or just walk up to it. The code I have so far does allow the engine to log hit event when an item is colliding or hitting anything, but the ApplyDamage function isn’t working properly. I assume there should be a function in place to fulfill the requirements by linking the item to its holder, but I am struggling to make this happen. Please enlighten me with your wisdom!
Here is the code I have now:
AItem::AItem()
{
// 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;
ObjectMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ObjectMesh"));
ObjectMesh->SetSimulatePhysics(true);
RootComponent = ObjectMesh;
ObjectMesh->OnComponentHit.AddDynamic(this, &AItem::OnHit);
}
Function for applying damage:
void AItem::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
auto MyOwner = GetOwner();
if (MyOwner == nullptr) return;
auto MyOwnerInstigator = MyOwner->GetInstigatorController();
auto DamageTypeClass = UDamageType::StaticClass();
UE_LOG(LogTemp, Warning, TEXT("Dealing damage"));
if (OtherActor && OtherActor != this, OtherActor != MyOwner)
{
UGameplayStatics::ApplyDamage(OtherActor, Damage, MyOwnerInstigator, MyOwner, DamageTypeClass);
/*UE_LOG(LogTemp, Warning, TEXT("Dealing damage"));*/
}
}
Thanks!