I’m trying to call a custom method when an overlap occurs. I have a sphere, and a box. The sphere moves around on its own with constant velocity bouncing off walls. The box is controlled by the player along one axis. When the ball hits the box I want it to flip its y velocity.
Based on answers I read here I initialize my ball with,
Aball::Aball()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("ROOT"));
RootComponent = SphereComponent;
VisibleComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VISIBLE"));
VisibleComponent->AttachTo(RootComponent);
SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &Aball::Paddle_Hit);
}
And Paddle_Hit looks like,
void Aball::Paddle_Hit(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
Velocity.Y = Velocity.Y*-1.0f;
}
With the following line for it in the header as well,
void Paddle_Hit(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
I also set up both objects to accept overlap,
I also set the movement updates to use the proper SetActorLocation I found here. For the sphere,
void Aball::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
FVector location = GetActorLocation();
if (location.X >= 240.0f || location.X <= -240.0f)
Velocity.X = Velocity.X*-1.0f;
SetActorLocation(location + Velocity*DeltaTime*100.0f, true);
}
For the box,
void Apaddle::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
if (!CurrentDirection.IsZero())
{
FVector location = GetActorLocation();
if ((location.X >= 200.0f && CurrentDirection.X<0.0f) || (location.X <= -200.0f && CurrentDirection.X>0.0f) || (location.X < 200.0f && location.X > -200.0f))
SetActorLocation(location + CurrentDirection*DeltaTime,true);
}
}
The problem is that when the box and sphere collide in game, nothing seems to happen. Am I missing something obvious?