About box collision with Pawn

I did googling and I can only find out that I may have to use Trigger volume with assigning the actor with box mesh (OnActorBeginOverlap?).

is this correct way of doing this?

because In Unity, if the player step into a box shaped trigger (or rigidbody cannot remember)… it can pulls function like OnCollisionEnter.

In my game, my character step into a box shape battle zone (invisible, but visible in debug mode) and a battle triggers.

when they mention about Trigger Volume, they mention about martinee, which i don’t need it for now.

anyone who can suggest about my 2D RPG game?

yes, the box component is fine as trigger volume, just set its collision properly to sense only player pawns (and exclude e.g. bullets), and use the above mentioned on-begin-overlap event

thank you sir!

is it possible to adjust the size the box easily in editor? or do i have to adjust it manually in Visual Studio?

anyone can show me simple code? after I did “CreateDefaultsSuboject” I don’t know what to do next… anyone?

Hi,

to add the component in C++

.h


UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Player Start")
    UBoxComponent* PlayerAreaVolume;

.cpp in the actor constructor (the actor must have an existing root component):


PlayerAreaVolume = CreateDefaultSubobject<UBoxComponent>(TEXT("PlayerAreaVolume"));
    PlayerAreaVolume->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);
    PlayerAreaVolume->SetBoxExtent(FVector(1000, 1000, 500), false);    // the size is twice of extent value!
    PlayerAreaVolume->SetCollisionEnabled(ECollisionEnabled::NoCollision);
    PlayerAreaVolume->SetWorldRotation(FRotator(0,0,0), false, nullptr, ETeleportType::None);

for the begin and end overlap events, in C++ you need to add delegates. the best you can do in similar cases to use Google :slight_smile: e.g. in this case with “ue4 C++ trigger box”, there are many useful links like this: https://answers.unrealengine.com/questions/187546/how-to-use-a-trigger-volume-with-c.html

moreover, it is very easy to do it all in Blueprint…

i see i will check it out. Thank you sivan. how to do it in blueprint?

I tried the link u gave me. UE4 said I cannot use IsAlreadyBound and AddDynamic… any solution?

solved add dynamic problem. but the function does not work… even my pawn stepped into other box, still no detection.

hey guys… I really need help. I even set box collision on my pawn… and ticked Generate Overlap Events During Level Streaming on my pawn and my collision box (actor) and still no overlap event… what could I missing this time?

oops i mean Generate Overlap Events.

This is the function I’m using.

.h


UPROPERTY()
	UBoxComponent* Box;

	UFUNCTION()
	void OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

	UFUNCTION()
	void OnOverlapEnd(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);

.cpp


YourClass::YourClass()
{
	Box = CreateDefaultSubobject<UBoxComponent>(TEXT("YourBox"));
	RootComponent = Box;
	Box->bGenerateOverlapEvents = true;
	Box->OnComponentBeginOverlap.AddDynamic(this, &YourClass::OnOverlapBegin);
	Box->OnComponentEndOverlap.AddDynamic(this, &YourClass::OnOverlapEnd);

 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = false;
}

void YourClass::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	
}

void YourClass::OnOverlapEnd(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{

}

thank you i will check it out soon

OnOverlapEnd working. strangely not OnverlapBegin… what could I missing…

I cannot point out why overlap begin does not work… is it because of my pawn or my other actor?

This is the documentation of the “OnBeginOverlap” function

Post here your function if possible.

Here is my code (i even almost copied it from documentation)

Here is my code…

BattleZone.h




#pragma once

#include "GameFramework/Actor.h"
#include "BattleZone.generated.h"

UCLASS()
class NOCOUNTRYFORTHEM_API ABattleZone : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ABattleZone(const FObjectInitializer& ObjectInitializer);

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

	UPROPERTY(VisibleAnywhere, Category = "Box")
	class UBoxComponent* CollisionBox;
	
	//void OnBeginTriggerOverlap(class UPrimitiveComponent* HitComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

	UFUNCTION()
		void OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

	UFUNCTION()
		void OnOverlapEnd(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);

};


BattleZone.cpp



ABattleZone::ABattleZone(const FObjectInitializer& ObjectInitializer)
	:Super(ObjectInitializer)
{
        CollisionBox = CreateDefaultSubobject<UBoxComponent>(TEXT("CollisionBox"));
	RootComponent = CollisionBox;
	CollisionBox->bGenerateOverlapEvents = true;
	CollisionBox->OnComponentBeginOverlap.AddDynamic(this, &ABattleZone::OnOverlapBegin);
	CollisionBox->OnComponentEndOverlap.AddDynamic(this, &ABattleZone::OnOverlapEnd);;

	//Set this actor to call Tick() Everyframe. YOu can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = false;
}

void ABattleZone::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	if ((OtherActor != nullptr) && (OtherActor != this) && (OtherComp != nullptr))
	{
		GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "Battle starts");
	}
}

void ABattleZone::OnOverlapEnd(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	if ((OtherActor != nullptr) && (OtherActor != this) && (OtherComp != nullptr))
	{
		GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "Battle End");
	}
}


Base_Character.h



class NOCOUNTRYFORTHEM_API ABase_Character : public APawn
{
// Sets default values for this pawn's properties
	ABase_Character(const FObjectInitializer& ObjectInitializer);
}


Base_Character.cpp



ABase_Character::ABase_Character(const FObjectInitializer& ObjectInitializer)
	:Super(ObjectInitializer)
{
 	// Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	CollisionBox = CreateDefaultSubobject<UBoxComponent>(TEXT("CollisionBox"));
	//RootComponent = CollisionBox;
	CollisionBox->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);
	CollisionBox->bGenerateOverlapEvents = true;

}


As you can see… BattleZone is an Actor with collision box and Base_Character is a Pawn with Paper2D Sprite with few statistic. they both suppose to overlap at beginning with message… but only messages pops out when they departs each other.

I am hopeless… if this is solved asap, I may reach my own deadline…

This is really odd. From what I’ve seem your code looks right.

Try rebuilding your project in Visual Studio (don’t know with this will work but who knows) and create the box collision with blueprints and see if the problem persists. Also, create a blank project and see if this happens too.

okay i will try

i used blue print (level blue print) it finally worked… what kind of socerry is this…it didn’t work last time… hmm… i need coffee about this situation.