Character hit on line trace

Hello,

I have an enemy at which I’m shooting using linetrace.
How can I make the enemy aware of this event?

Should I trigger a hit event for the component or what will be best solution for this?

UPrimitiveComponent * otherComp = HitResult.GetComponent();

Or will it be better to get the class from the component (if this is even possible) and execute some function then?

Thank you

Hi xhallix,

There are multiple ways to do this, but if you’re trying to get the enemy to react, you could do something similar to this function for making a projectile deal damage to an enemy/player by letting them know that they’ve been hit and telling them to run a function to hurt themselves essentially:

void AMyProjectile::ReceiveHit(class UPrimitiveComponent* MyComp, AActor* Other, class UPrimitiveComponent* OtherComp, bool bSelfMoved, FVector HitLocation, FVector HitNormal, FVector NormaImpulse, const FHitResult & Hit)
 {
     Super::ReceiveHit(MyComp, Other, OtherComp, bSelfMoved, HitLocation, HitNormal, NormaImpulse, Hit);
     AMyCharacter* placeholder = Cast<AMyCharacter>(Other);
     if (placeholder)
     {
         placeholder->ReceiveDamage(ProjectileDamage);
     }
 Destroy();
 }

This particular function, ReceiveHit, is a collision detection event that is being overridden from a base class in the engine, but it can give you a good idea. This particular projectile is only suppose to deal damage to a certain class, therefore I have it store a cast to that particular class using the ‘Other’ (The other actor that is being hit) variable in a pointer. This will determine if the thing being hit is indeed that class. I then use an If statement to check and have the pointer call ReceiveDamage on that object so that it gets hurt.

Hope this helps!

Matthew Clark

Hi Matthew,

that was the solution I’m looking for :slight_smile:
What I did (because I did not know better) was

	if (otherActor->IsA(AEnemyy::StaticClass()))
	{
               //blueprint implementable event
		this->AttackEnemy(otherActor);
	}

and then casting the character in the blueprint.
But this seems to be a bit to overcomplicated.

Thanks a lot for your support,
A step deeper again for me in UE :slight_smile: