x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Actor destroying self on spawn

So I'm having a bit of a problem with collision detecting. very basic right? anyway I can detect an on overlap perfectly fine right. I create a bullet actor on a level and when it collides with something it destroys itself. the problem is that it now seems to destroy itself with spawned in by a player.

Anyway this is the header UCLASS() class TANKS2_API ABullet : public AActor { GENERATED_BODY()

public: // Sets default values for this actor's properties ABullet();

protected: // Called when the game starts or when spawned virtual void BeginPlay() override;

 UPROPERTY(EditAnywhere, BlueprintReadWrite)
     UStaticMeshComponent* body;


public: // Called every frame virtual void Tick(float DeltaTime) override;

 UPROPERTY(EditAnywhere)
     USceneComponent* CollisionRoot;

private:

 //Collsion Sphere Pointer
 //USphereComponent* Collision;
 UPROPERTY(EditAnywhere)
     UShapeComponent * Collision;

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


};

and the cpp

ABullet::ABullet()

{

 // 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;


 CollisionRoot = CreateDefaultSubobject<USceneComponent>(TEXT("CollisionRoot"));
 RootComponent = CollisionRoot;

 //initialise the static mesh
 body = CreateDefaultSubobject<UStaticMeshComponent>("body");
 //this->RootComponent = body;
 body->AttachTo(CollisionRoot);


 //part of collision detection
 this->Collision = CreateDefaultSubobject<UBoxComponent>(TEXT("ObjectName"));
 this->Collision->SetGenerateOverlapEvents(true);
 this->Collision->SetWorldScale3D(FVector(1.f, 1.f, 1.f));
 this->Collision->OnComponentBeginOverlap.AddDynamic(this, &ABullet::OnBeginOverlap);
 this->Collision->AttachToComponent(this->CollisionRoot, FAttachmentTransformRules::SnapToTargetNotIncludingScale);


}

// Called when the game starts or when spawned void ABullet::BeginPlay() { Super::BeginPlay();

}

// Called every frame void ABullet::Tick(float DeltaTime) { Super::Tick(DeltaTime);

}

//CollisionFunction void ABullet::OnBeginOverlap(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) {

 if ((OtherActor != nullptr) && (OtherActor != this) && (OtherComp != nullptr))
 {
     Destroy();
 }

}

Product Version: UE 4.20
Tags:
more ▼

asked Aug 18 '18 at 07:20 AM in C++ Programming

avatar image

Hides_In_Bushes_
5 1 2

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

It may be overlapping static geometry in your level or with whoever is shooting the bullet (if you're spawning it from other actor at it's location) when you spawn it. You will have to check that OtherActor is not the actor who shot the bullet or make sure that you're spawning it where is nothing to collide with. You should create a custom collision profile that filters anything that is not relevant for your actor collision too. You can do so in Project Settings -> Collision and you can set the profile in your collider shape by using Collision->SetCollisionProfile("your profile name").

Cheers!

more ▼

answered Aug 18 '18 at 03:47 PM

avatar image

Sveitar
1k 8 24 26

avatar image Hides_In_Bushes_ Aug 19 '18 at 01:14 AM

Not quite what I needed but, helped me figure it out. I forgot to change the world scale from something else. the box size was so big it overlapped the thing that was spawning it and destroyed itself.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question