USphereComponent as sphere collider - C++

Hello,
I am trying to make my AI Attack with sphere collider so that the attacks will sync with the animation.
the problem is when i try to set the sphere to a socket on the skeleton it wont go there and the radius keeps being realy small but is set it to 400.f.
The code does create a sphere colider but it is under the character, In addition There are no details in the character blueprint when i select the sphere.
If any one can help me understand why that would be grate.
.h


UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Attacking Collusion")
    class USphereComponent* AttackingCollusion;

.cpp


    AttackingCollusion = CreateDefaultSubobject<USphereComponent>(TEXT("Attacking Cullusion"));
    AttackingCollusion->InitSphereRadius(400.f);
    AttackingCollusion->AttachTo(GetMesh(),"AttackingCollision",EAttachLocation::SnapToTarget);


Hey there!

I think this guy’s tutorial walks through exactly what you are trying to accomplish: Unreal Engine 4 Tutorial - AI - Part 8 Melee Attacks (Animation) - YouTube - I clipped it so it would start at the relevant part for you. Here is the next tutorial in the series as well that finishes up the melee attack hit collision: Unreal Engine 4 Tutorial - AI - Part 9 Melee Attacks (Behaviour) - YouTube

Here is a little clip I made of the results, could save you some time watching the videos if this isn’t what you want:

Hope this helps! Have a great day!

After i closed and opened the project it doest work but when i press f5 in vs2017
is stops here


bool USceneComponent::AttachToComponent(USceneComponent* Parent, const FAttachmentTransformRules& AttachmentRules, FName SocketName)
{
    FUObjectThreadContext& ThreadContext = FUObjectThreadContext::Get();
    if (ThreadContext.IsInConstructor > 0)
    {
        // Validate that the use of AttachTo in the constructor is just setting up the attachment and not expecting to be able to do anything else
        ensureMsgf(!AttachmentRules.bWeldSimulatedBodies, TEXT("AttachToComponent when called from a constructor cannot weld simulated bodies. Consider calling SetupAttachment directly instead."));
        ensureMsgf(AttachmentRules.LocationRule == EAttachmentRule::KeepRelative && AttachmentRules.RotationRule == EAttachmentRule::KeepRelative && AttachmentRules.ScaleRule == EAttachmentRule::KeepRelative, TEXT("AttachToComponent when called from a constructor is only setting up attachment and will always be treated as KeepRelative. Consider calling SetupAttachment directly instead."));
        SetupAttachment(Parent, SocketName);
        bShouldSnapLocationWhenAttached = false;
        bShouldSnapRotationWhenAttached = false;

        return true;
    }


With this exeption thrown UE4Editor.exe has triggered a breakpoint.
If i press continue it works (I didnt set any break points there).

Edit: i removed the attach from the contractor and it works fine :slight_smile:


 
 AttackingCollusion->SetupAttachment(RootComponent); 

Try this code when you initialize the components, don’t forget to add them to the root node.

1 Like

just an alternative suggestion for you :slight_smile:

Rather than having a sphere collision component that is always in existence you can fire off sphere traces from the bone location instead.

Create a new AnimNotifyState and add it to you attack animation montage, adjust the start and end of the state to where you want the collision to fire in the animation, then on the tick function in the anim notify you can call a function that does a multi sphere trace from the GetMesh()->BoneLocation(BoneName) (i can’t remember exactly what that function is called so check that) and handles the collision.

Sounds good i’ll check that :slight_smile:

Just thought I probably should have mentioned a gotcha I ran into with that method.

On the sphere trace it didnt seem to work when the start and end trace locations were the same, so for the end trace i just upped the value by the forward vector by a tiny amount and then all good :slight_smile: