Collision – picking up objects using Overlap

Item pickup is a pretty important thing to get down cleanly. In this recipe, we'll outline how to get item pickups working using Overlap events on Actor Component primitives.

Getting ready

The previous recipe, Collisions: Letting Objects pass through each other using Ignore, describes the basics of collisions. You should read it for background before beginning this recipe. What we'll do here is create a New Object Channel... to identify Item class objects so that they can be programmed for overlaps only with the player avatar's collision volume.

How to do it...

  1. Start by creating a unique collision Channel for the Item object's collision primitive. Under Project Settings | Collision, create a new Object Channel by going to New Object Channel…
    How to do it...
  2. Name the new Object Channel as Item.
  3. Take your Item actor and select the primitive component on it that is used to intersect for pickup with the player avatar. Make the Object Type of that primitive an Item class Object Type.
  4. Check the Overlap checkbox against the Pawn class Object Type as shown in the following screenshot:
    How to do it...
  5. Ensure that the Generate Overlap Events checkbox is checked.
    How to do it...
  6. Take the player actor who will pick up the items, and select the component on him that feels for the items. Usually, this will be his CapsuleComponent. Check Overlap with the Item object.
    How to do it...
  7. Now the Player overlaps the item, and the item overlaps the player pawn. We do have to signal overlaps it both ways (Item Overlaps Pawn and Pawn Overlaps Item) for it to work properly. Ensure that Generate Overlap Events is also checked for the Pawn intersecting component.
  8. Next we have to complete the OnComponentBeginOverlap event for either the item or the Player's pickup volume, using either Blueprints or C++ code.
    1. If you prefer Blueprints, in the Events section of the Details pane of the Coin's intersectable Component, click on the + icon beside the On Component Begin Overlap event.
      How to do it...
    2. Use the OnComponentBeginOverlap event that appears in your Actor Blueprint diagram to wire-in Blueprints code to run when an overlap with the Player's capsule volume occurs.
    3. If you prefer C++, you can write and attach a C++ function to the CapsuleComponent. Write a member function in your player's avatar class with a signature as follows:
      UFUNCTION(BlueprintNativeEvent, Category = Collision)
      void OnOverlapsBegin( UPrimitiveComponent* Comp, AActor* OtherActor,
      UPrimitiveComponent* OtherComp, int32 OtherBodyIndex,
      bool bFromSweep, const FHitResult& SweepResult );

      Tip

      In UE 4.13, the OnOverlapsBegin function's signature has changed to:

      OnOverlapsBegin( UPrimitiveComponent* Comp, AActor* OtherActor,UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitREsult& SweepResult );
    4. Complete the implementation of the OnOverlapsBegin() function in your .cpp file, making sure to end the function name with _Implementation:
      void AWarrior::OnOverlapsBegin_Implementation( AActor*
      OtherActor, UPrimitiveComponent* OtherComp,
      int32 OtherBodyIndex,
      bool bFromSweep, const FHitResult& SweepResult )
      {
        UE_LOG(LogTemp, Warning, TEXT( "Overlaps began" ) );
      }
    5. Then, provide a PostInitializeComponents() override to connect the OnOverlapsBegin() function with overlaps to the capsule in your avatar's class as follows:
      void AWarrior::PostInitializeComponents()
      {
        Super::PostInitializeComponents();
        if(RootComponent )
        {
          // Attach contact function to all bounding components.
          GetCapsuleComponent()->OnComponentBeginOverlap.AddDynamic( this, &AWarrior::OnOverlapsBegin );
          GetCapsuleComponent()->OnComponentEndOverlap.AddDynamic( this, &AWarrior::OnOverlapsEnd );
        }
      }

How it works…

The Overlap event raised by the engine allows code to run when two UE4 Actor Components overlap, without preventing interpenetration of the objects.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.183.221