Attack/Melee System c++

Good Evening,

I am currently working on an RPG, but I am stuck on creating a Attack/Melee System with C++, since I don’t know how to get started on this topic. Right now I have a basic Character that can move around, jump, sprint, dash, etc and an AI that follows the player if the Player is seen by the AI. Both of them also have Health and the Player has a PlayerInputComponent for the left mouse button, but the function that is bound to that button doesn’t contain any code right now. I have read that either Collision or Raycast can be used for attacking, but which one would be better to use and how would I be able to use them for attacking?

Hey there, doing a line trace on the blade of the weapon is usually the way to go. The main idea is to create a custom AnimNotify State that runs the line trace on the frames you want to detect collision in the attack animations,t hat way you can say that on Attack 1 try to detect collision from frame 20 to frame 35. The only that you need to take into account is managing hitting the same character over and over on the same animation. To fix this, when you detect a first hit you need to add that character to an array of hits (that gets cleared once the animation is over usually) and only process the damage if the current hit character is not on the array. Hope this helps :slight_smile:

Thank you very much for your reply. Would I have to make a cpp for the sword and then have the line trace in there or should it be in the character cpp file?

When you create the custom AnimNotifyState and override Received Notify Tick you get the Skeletal Mesh that it’s being used with in the animation, you just cast that to the character and from there you can call a c++ function that does something like this:

void APlayerCharacter::TraceWeapon()
{
     if(CurrentWeapon == nullptr)
          return;

    UStaticMeshComponent * WeaponMesh = CurrentWeapon->GetMesh();

    FVector TraceStart = WeaponMesh->GetSocketLocation("TraceStart");
    FVector TraceEnd = WeaponMesh->GetSocketLocation("TraceEnd");

   if(GetWorld()->LineTrace(TraceStart, TraceEnd, HitResult))
   {
         if(HitResult.Actor->IsA<ACharacter>())
              UGameplayStatics::ApplyPointDamage(Damage, HitResult.Actor);
   }
}

So all you need to setup is the TraceStart and TraceEnd sockets in the static meshes and you should be good to go.

So I managed to get the AnimNotifyState to cast the skeletal mesh to my player, but the thing I am getting confused about now is CurrentWeapon. What does it represent? Is that the name you chose for the skeletal mesh that gets casted? I am pretty new to all this, so apologies for asking a lot of questions.

No problem :slight_smile: Current Weapon can be an actor representing the weapon or the mesh component for it in your character (if it is, then ignore Line 6 and use CurrentWeapon instead of WeaponMesh in Line 8 and 9).

It can be from a blueprint aswell, since they also derive from AActor, but if you are doing the trace code in c++ then you need to be able to access the Mesh inside of the blueprint and it’s easier to access it if you create it in c++. You can still do it with blueprints, you just get a reference of the mesh component by using FindComponentByClass in the trace code.

Oh, so I have to create a c++ actor file for the sword instead of just a blueprint one?

Everything seems to be working fine now, but I had to make a few changes to the linetrace because it told me that there is nothing called LineTrace in UWorld, so I used LineTraceByChannel instead. How would I be able to use LineTrace instead of LineTraceByChannel? I included this #include “Runtime/Engine/Classes/Engine/World.h” for Uworld , but it didn’t help.

For some reason it is registering my character as well, I think. I am trying it out with a DebugMessage and the message gets spammed even if I don’t hit anything

The code that i did was pseudocode, so yeah use LineTraceByChannel. Eventually you might need to do a Multitrace in case you hit multiple enemies at the same time.

On the trace you can add the character to the ignore actor list :slight_smile:

It’s working perferctly now. I greatly appreciate the help I have received from you. Thank you very much :slight_smile: Does Multitrace only apply to the same Object type? or could it be used for multiple ones, like Actors and destructibles?

Good to know :slight_smile: Multitrace can be used by object type or channel type, just like single trace.