Sword is just colliding with AI, so it doesn't receive damage

Hi, I’ve done a simply combact sytem:

  1. Character attacks AI with a Sword with a Collider box
  2. AI has 2 spheres: ViewSphere (if player is in range of the sphere AI follows him) and AttackRangeSphere (if player is in range of sphere AI attack him);
  3. AI has a time he must wait before attack again;

But I’ve a problem…when AI is near and Character isn’t attacking, sword is just colliding with AI…I’ve added an “IF(IsAttacking)” (if character is Attacking it inflicts damage to AI, else it return), but sometimes he done animation of attack but AI doesn’t take damage…How can I resolve? Please, I’m desperate…

This is my video in game:

thanks!!

  • First question, are you using an animation blueprint to do animations, or are you just calling play animation from within the characters blueprint?
  • Second if you are using an animation blueprint, are you familiar with animation notifies?

If you do know how to make animation notifies and make an animation blueprint, I would add notifies when he starts his swing and when hes finished his swing to enable / disable collision on the weapon. Then you don’t need to have a bool for when hes attacking because it will only detect collision during the swing.

Yes, I’m using animation blueprint and I’m using animation notifies

oh…that’s true!! how can I enable/disable collision??

Get a reference to the collider, call pull off a pin and do SetCollisionEnabled, and select NoCollision with the drop down, or Quary Only.

Is the collider actually attached to the sword, Is the collider big enough, Is the enemies collider big enough. You tun off under rendering for the collider to hide it in game, might help debug.

I’ve done it in Blueprint, but it sometimes doesn’t hit AI yet

yes, all collider are big enought…I think it’s all correct…

Can I see your logic for detecting and applying damage?

Here you can see :

AMyProject4Character::AMyProject4Character()
{

	Sword = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Character_Sword"));
	Sword->bCastDynamicShadow = false;
	Sword->CastShadow = false;
	Sword->AttachTo(GetMesh(), TEXT("weapon"), EAttachLocation::SnapToTargetIncludingScale, true);
	Sword->OnComponentHit.AddDynamic(this, &AMyProject4Character::OnHit);

}

//////////////////////////////////////////////////////////////////////////


void AMyProject4Character::AttackSimple()
{
	if (!isTakingDamage)
	{
		isAttacking = true;
	}
}

void AMyProject4Character::StopAttackSimple()
{
	Colpiti.Empty();

float AMyProject4Character::TakeDamage(float DamageAmount, struct FDamageEvent const& DamageEvent, class AController* EventInstigator, class AActor* DamageCauser)
{
	UE_LOG(LogTemp, Warning, TEXT("danno subito"));
	if (isTakingDamage == false )
	{
		isTakingDamage = true;
		float ActualDamage = Super::TakeDamage(DamageAmount, DamageEvent, EventInstigator, DamageCauser);
		HealtPoints = HealtPoints - ActualDamage;
		if (HealtPoints <= 0) {
			OnDeath();
		}
		return ActualDamage;
	}
	return 0;
}

void AMyProject4Character::OnDeath()
{
	Destroy();
}

void AMyProject4Character::OnHit(AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
	if (OtherComp != OtherActor->GetRootComponent())
	{
		return;
	}
	if (OtherActor != this && isAttacking && !Colpiti.Contains(OtherActor))
	{
		OtherActor->TakeDamage(AttackDamage, FDamageEvent(), NULL, this);
		Colpiti.Add(OtherActor);
	}

}

Sword->OnComponentHit.AddDynamic(this, &AMyProject4Character::OnHit);

You are using the sword mesh as the collider, not your attached collider, and you are checking for OnComponentHit instead of OnComponentBeginOverlap.

The video was hard to tell, so it looks like you only have the meshes collider instead of having an attached capsule collider. This is fine, just use OnComponentBeginOverlap in place of OnComponentHit on line 8 in your .cpp and let me know how that turns out.

Ok so now you want to limit it to only dealing damage once correct?

Ok well if that does happen to be the case, you can set a tag on the collider you want to use to register taking hits with and then instead of using the OtherActor from the hit results you can use the OtherComp and check to see if it has the tag. So set a tag like, Enemy or something.

but sword mesh has his collider…
so how can I resolve?

well…I’ve just tryied to done it…but I’ve got a problem:
AI has two spheres…If I do attack when I’m in a USphere of AI it does take damage…

Now I’ve done it…I added a UBoxComponent attached to Sword…and done overlap method on him…but problem remains…I’ve seen the problem…if at start of attack sword is just in capsule component of AI attack doesn’t inflict damage

hmm well you could also try making a collider that just sittings in front of the player and when the weapon swings just do a Get All Overlapping Components.