Hello!!
I want to have two different overlap functions for two different components in C++, for some reason, I can’t get this to work. The body collision works, but the fist ones dont. any ideas what is happening or what I am missing?
.h
//Collision function for the fists
UFUNCTION()
void FistCollision(AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult);
//Collision function for the Character
UFUNCTION()
void BodyCollision(AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult);
UFUNCTION()
void BodyCollisionExit(AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex);
.cpp
//Create a collision for the right hand/fist
RightFistCollision = ObjectInitializer.CreateDefaultSubobject<UBoxComponent>(this, TEXT("RightFistCollision"));
RightFistCollision->AttachTo(GetMesh(), "RightHandSocket");
RightFistCollision->OnComponentBeginOverlap.AddDynamic(this, &ABornImmortalCharacter::FistCollision);
RightFistCollision->SetCollisionProfileName(TEXT("NoCollision"));
//Create a collision for the left hand/fist
LeftFistCollision = ObjectInitializer.CreateDefaultSubobject<UBoxComponent>(this, TEXT("LeftFistCollision"));
LeftFistCollision->AttachTo(GetMesh(), "LeftHandSocket");
LeftFistCollision->OnComponentBeginOverlap.AddDynamic(this, &ABornImmortalCharacter::FistCollision);
LeftFistCollision->SetCollisionProfileName(TEXT("NoCollision"));
CollisionComp = ObjectInitializer.CreateDefaultSubobject<UBoxComponent>(this, TEXT("CollisionComp"));
CollisionComp->AttachTo(RootComponent);
CollisionComp->OnComponentBeginOverlap.AddDynamic(this, &ABornImmortalCharacter::BodyCollision);
CollisionComp->OnComponentEndOverlap.AddDynamic(this, &ABornImmortalCharacter::BodyCollisionExit);
//FUNCTION FOR FIST COLLISIONS
void ATestCharacter::FistCollision(AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
{
if (OtherActor && OtherActor != this)
{
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, "Derp");
ATestActor *Char = Cast<ATestActor>(OtherActor);
if (Char)
{
Char->GetMesh()->AddImpulseAtLocation(FVector(0, 0, 10000), Char->GetActorLocation());
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, "Ouch");
}
AEnemy *Enemy = Cast<AEnemy>(OtherActor);
if (Enemy)
{
Enemy->GetMesh()->AddImpulseAtLocation(FVector(0, 0, 10000), Enemy->GetActorLocation());
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, "Derp");
}
}
}
void ATestCharacter::BodyCollision(AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
{
AWeapon *Weapon = Cast<AWeapon>(OtherActor);
if (OtherActor && OtherActor != this)
{
if (Weapon)
{
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, "Enter");
bIsWeaponPickup = true;
WeaponPickup = Weapon;
}
}
}
void ATestCharacter::BodyCollisionExit(AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex)
{
AWeapon *Weapon = Cast<AWeapon>(OtherActor);
if (OtherActor && OtherActor != this)
{
if (Weapon)
{
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, "EXIT");
bIsWeaponPickup = false;
WeaponPickup = NULL;
}
}
}
KEEP IN MIND
I also turn on and off the “NoCollision” and switch it to “OverlapAllDynamic” when the fist attack happens