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.
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.
Item
object's collision primitive. Under Project Settings | Collision, create a new Object Channel by going to New Object Channel…Item
.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.Pawn
class Object Type as shown in the following screenshot:CapsuleComponent
. Check Overlap with the Item
object.Item
Overlaps Pawn
and Paw
n Overlaps Item
) for it to work properly. Ensure that Generate Overlap Events is also checked for the Pawn
intersecting component.OnComponentBeginOverlap
event for either the item or the Player's pickup volume, using either Blueprints or C++ code.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.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 );
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" ) ); }
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 ); } }
3.135.183.221