Download

OnActorHit, delegate how do they work?

Hi

I have a very simple character class in my project, the goal is to make my character bounce when it hits stuff, in a very non-physical manner. So I thought that I could use OnActorHit and then give my character a push when hitting stuff. So I probably want some data about the collision, e.g. a normal and the force of the collision. But I can’t figure out how to write the code and I can’t for the life of me find any examples, tutorials or similar on how to do it. I’m using Unreal Engine 4.7.2. So I would be very happy if someone could give me a small example how to setup a delegate for OnActorHit (if that is the most suitable functions to use in my case).

Hi look at the answers at those links:

https://answers.unrealengine.com/questions/27808/how-to-push-add-force-to-character-through-bluepri.html

https://answers.unrealengine.com/questions/110885/adding-force-to-pawn.html

They have some insightful information that migth give you a starting point.

As for the the event you can use any collision related events, like Overlap, in your Characters blueprint right click, add the event and use it as the starting event described in the links above.

As for C++ only look the the documentation here: https://docs.unrealengine.com/latest/INT/Gameplay/ClassCreation/CodeOnly/index.html



void UYourActorComponent::InitializeComponent()
{
    FScriptDelegate Delegate;
    Delegate.BindUFunction(this, "OnActorBump");
    GetOwner()->OnActorHit.AddUnique(Delegate);
}




UCLASS(....)
class UYourActorComponent : public UActorComponent
{
....
    UFUNCTION()
    void OnActorBump(AActor* SelfActor, AActor* OtherActor, FVector NormalImpulse, const FHitResult& Hit);
....
};




void UYourActorComponent::OnActorBump(AActor* SelfActor, AActor* OtherActor, FVector NormalImpulse, const FHitResult& Hit)
{
    // Handle....
}