For some reason, my SphereComponent doesn’t spawn, the actor spawns perfectly fine, but I cannot see the SphereComponent nor interact with its OnOverlapBegin. Can someone identify the problem?
In the header:
class USphereComponent* SphereVolume;
In the cpp:
AMyActor::AMyActor()
{
// 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;
bReplicates = true;
ActorDestroyTime = 5.0f;
(.........)
SphereVolume = CreateDefaultSubobject<USphereComponent>(TEXT("Timewarp Volume"));
SphereVolume->InitSphereRadius(800.0f);
SphereVolume->AttachParent = RootComponent;
SphereVolume->ShapeColor = FColor::;
SphereVolume->SetVisibility(true);
SphereVolume->SetIsReplicated(true);
SphereVolume->SetCollisionEnabled(ECollisionEnabled::NoCollision);
SphereVolume->SetCollisionResponseToAllChannels(ECR_Ignore);
SphereVolume->SetCollisionResponseToChannel(ECC_Pawn, ECR_Overlap);
SphereVolume->OnComponentBeginOverlap.AddDynamic(this, &AMyActor::OnOverlapBegin);
SphereVolume->OnComponentEndOverlap.AddDynamic(this, &AMyActor::OnOverlapEnd);
}
void AMyActor::OnOverlapBegin(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (OtherComp)
return;
if (OtherActor->IsA(AMyCharacter::StaticClass()))
{
if (OtherActor && (OtherActor != this))
{
if (Role > ROLE_Authority)
{
//Slow him down
(AMyCharacter*)(OtherActor);
}
GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Green, "OVERLAAAAAAAAAAPPPPPPP");
}
}
}
void AMyActor::OnOverlapEnd(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
if (OtherActor->IsA(AMyCharacter::StaticClass()))
{
if (OtherActor && (OtherActor != this))
{
GEngine->AddOnScreenDebugMessage(1, -1.0f, FColor::Green, "STOP OVERLAAPPP");
}
}
}
I’m not sure what version this changed in but this is no longer the case. You no longer need a reference to the FObjectInitializer to create a DefaultSubobject in the default constructor.
I tried it, had tried it before as well, the problem persists…
RootComponent = SphereVolume;
I actually tried setting up a DebugSphere on the component’s location (so retrieve the component’s location and spawn it there), and it spawns fine, the only problem seems to be the sphere itself isn’t initialized, but should be, that code was taken from the source code.
From the Default projectile class:
AMyProjectile::AMyProjectile()
{
// Use a sphere as a simple collision representation
CollisionComp = CreateDefaultSubobject(TEXT(“SphereComp”));
CollisionComp->InitSphereRadius(5.0f);
CollisionComp->BodyInstance.SetCollisionProfileName(“Projectile”);
CollisionComp->OnComponentHit.AddDynamic(this, &AL2Projectile::OnHit);
…
USphereComponent does not have a mesh to render. You’ll be able to see it’s bounds in the editor but not while playing.
SphereVolume->SetCollisionEnabled(ECollisionEnabled::NoCollision); is incorrect. You want SphereVolume->SetCollisionEnabled(ECollisionEnabled::QueryOnly); for overlaps. Look here: ECollisionEnabled::Type
Actually I can’t even see the sphere, so I do not know if I am entering or not. Sometimes when I’m walking around the map I see - almost as if randomly - that I am entering it (have a debug message there).
So what I have there currently is:
SphereVolume->InitSphereRadius(1000.0f); will make your sphere have a diameter of 20 meters. Also, don’t spawn inside of the sphere. While playing type “show collision” in to the command console. This should render the collision bounds and help you get a better idea of what is going on.
Oh that helped a lot! I can see the volume now, its being spawned correctly - why can’t I see it normally though? Its visibility is set to true, so I should be able to see it.
However, its not registering overlap events nor does it get destroyed when the actor is destroyed. Do I need to override the destroy method?
Edit: Apparently its generating hit events, the default projectile from the Shooter game bounces off from it…
The reason the collision isn’t showing may be due to the Hidden in Game variable. This is separate from the Visible variable as it only affects its visibility at runtime. You should be able to set it to false in C++ as you do with other variables.
Thank you , I’m now able to see it. Overlaps are fixed as well.
Now only two problem remain:
the volume is generating collision against projectiles, meaning, from the ShooterGame example, the projectiles are hitting the volume and coming back. Any idea on how to disable it?
the volume doesn’t get destroyed when the actor is destroyed
If you look at the Collision section of the details panel for this SphereComponent, you can select “Custom” as a preset. If you do this, you can set the Projectile channel to be ignored for collision. If the projectile channel doesn’t exist, you can add your own custom channel under Project Settings → Collision. You can then apply the collision channel to your projectile.
For the volume being destroyed, is it calling Destroy on itself?