Hey Guys,
I am stuck here at a simple scenario:
- Shooting a Raycast with the Mouse Cursor through the screen
- FHitResult returns the Actor of the HitResult.
- TakeDamage() shall add Damage to the Actor in the Scene.
So is this possible or is the FHitResult just a copy of the actual object and modifying it doesn’t affect the actual Actor in the game?
I hope my question is clear. Thank you alot in advance
void URaycastComponent::ShootRaycast()
{
/// LINE TRACE and see if we reach any actors with physics body collision channel set
FHitResult HitResult = GetFirstPhysicsBodyInReach();
auto ActorHit = HitResult.GetActor();
if (ActorHit)
{
FString ObjectName = ActorHit->GetName();
UE_LOG(LogTemp, Warning, TEXT("Hit Object: %s"), *ObjectName)
DestroyObject(HitResult); // DOESNT CHANGE THE OBJECT
}
}
void URaycastComponent::DestroyObject(FHitResult &HitResult)
{
// Create a damage event
TSubclassOf<UDamageType> const ValidDamageTypeClass = TSubclassOf<UDamageType>(UDamageType::StaticClass());
FDamageEvent DamageEvent(ValidDamageTypeClass);
const float DamageAmount = 10000.0f;
HitResult.GetActor()->TakeDamage(DamageAmount, DamageEvent, PlayerController,GetOwner());
HitResult.Actor->TakeDamage(DamageAmount, DamageEvent, PlayerController, GetOwner());
}