# Syntax for line trace hitting and applying damage to characters

**URL:** <https://forums.unrealengine.com/t/syntax-for-line-trace-hitting-and-applying-damage-to-characters/406305>\
**Category:** Character & Animation\
**Tags:** line-trace, apply-damage, syntax, unreal-engine, character-collision, CPP, question\
**Created:** [October 6, 2017, 2:49am UTC](https://forums.unrealengine.com/t/syntax-for-line-trace-hitting-and-applying-damage-to-characters/406305 "2017-10-06T02:49:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dougemmedhen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@dougemmedhen](https://forums.unrealengine.com/u/dougemmedhen)\
**Post date:** [October 6, 2017, 2:49am UTC](https://forums.unrealengine.com/t/syntax-for-line-trace-hitting-and-applying-damage-to-characters/406305/1 "2017-10-06T02:49:24Z")

</div>

I’m at my wit’s end, I have a weapon line trace that applies damage to destructible meshes, now I’m trying to figure out how to apply damage to characters. Here’s what I got:  
ACharacter \*person= Cast(hit.getactor());  
If(person-\>Actorhastag (FName (“RegularDude”)  
{  
}

That’s it. What is the syntax for actually hitting the character/capsule component? Please help me. The sooner I get this taken care of the better

---

<div class="post-metadata">

**Author:** ![Dune](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Dune](https://forums.unrealengine.com/u/Dune)\
**Post date:** [October 6, 2017, 1:27pm UTC](https://forums.unrealengine.com/t/syntax-for-line-trace-hitting-and-applying-damage-to-characters/406305/2 "2017-10-06T13:27:53Z")

</div>

Do you have the actual code you are using?

```
ACharacter* Person = Cast<ACharacter>(hit.getactor()); 
if (Person)
{
if (Person->ActorHasTag(TEXT("RegularDude")))
{

}
}
```

---

<div class="post-metadata">

**Author:** ![dougemmedhen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@dougemmedhen](https://forums.unrealengine.com/u/dougemmedhen)\
**Post date:** [October 6, 2017, 4:18pm UTC](https://forums.unrealengine.com/t/syntax-for-line-trace-hitting-and-applying-damage-to-characters/406305/3 "2017-10-06T16:18:38Z")

</div>

Yes, I have the code that checks if the line trace hits something that’s not null. The problem is I don’t know the syntax for applying damage to a character as intelesense doesn’t have a take damage function like I did when I checked to see if I hit a destructible mesh.

---

<div class="post-metadata">

**Author:** ![TestyRabbit](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TestyRabbit](https://forums.unrealengine.com/u/TestyRabbit)\
**Post date:** [October 6, 2017, 9:07pm UTC](https://forums.unrealengine.com/t/syntax-for-line-trace-hitting-and-applying-damage-to-characters/406305/4 "2017-10-06T21:07:25Z")

</div>

So all Actors have a [TakeDamage() function](https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/GameFramework/AActor/TakeDamage/index.html). I’m not exactly sure what the problem is, you shouldn’t have any issue calling Person-\>TakeDamage(…) after you make sure “Person” represents who you wanted.

---

<div class="post-metadata">

**Author:** ![dougemmedhen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@dougemmedhen](https://forums.unrealengine.com/u/dougemmedhen)\
**Post date:** [October 11, 2017, 9:31pm UTC](https://forums.unrealengine.com/t/syntax-for-line-trace-hitting-and-applying-damage-to-characters/406305/5 "2017-10-11T21:31:11Z")

</div>

Here is the entire bullet hit/fire function during it’s tick:

void ABullet::Tick( float DeltaTime )  
{  
Super::Tick( DeltaTime );

```
FHitResult hit;

FVector StartTrace = this->GetActorLocation();

FVector EndTrace = (Velocity * DeltaTime) + StartTrace; 
EndTrace.Z += this->GetActorRotation().Pitch;

FCollisionQueryParams params;

params.AddIgnoredActor(this);

if (GetWorld()->LineTraceSingleByChannel(hit,StartTrace,EndTrace, ECC_Visibility,params))
{
	if (hit.GetActor())
	{
		DrawDebugSolidBox(GetWorld(), hit.ImpactPoint, FVector(10.f), FColor::Blue, true);
		ADestructibleActor *Mesh = Cast<ADestructibleActor>(hit.GetActor());
		if (Mesh)
		{
			Mesh->GetDestructibleComponent()->ApplyRadiusDamage(10.f, hit.ImpactPoint, 32.f, 10.f, false);
		}
		ATheMeleeDude *person = Cast<ATheMeleeDude>(hit.GetActor());
		
		if (person)
		{
			person->health = person->DamageResistance - damage;
			if (GEngine)
			{
				GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, "Poop");
			}
			
		}
	}
	Destroy();
}
else
{
	bulletexpire += DeltaTime;
	DrawDebugLine(GetWorld(), StartTrace, EndTrace,FColor(0.f,-bulletexpire * 80.f,100.f),true);
	SetActorLocation(EndTrace);
	Velocity += FVector(0.f, 0.f, 200.f) * DeltaTime;
}

if (bulletexpire > 3)
{
	Destroy();
}

```

}

When I cast to TheMeleeDude, the if statement doesn’t run. Any reason why?

---

<div class="post-metadata">

**Author:** ![dougemmedhen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@dougemmedhen](https://forums.unrealengine.com/u/dougemmedhen)\
**Post date:** [October 13, 2017, 5:21am UTC](https://forums.unrealengine.com/t/syntax-for-line-trace-hitting-and-applying-damage-to-characters/406305/6 "2017-10-13T05:21:39Z")

</div>

Mark this as resolved I got my trace hitting the character.
