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"

C++ Collision detection not triggering

I'm having trouble with collision detection in C++. I've literally spent hours trying to get it to work and still nothing (I've reviewed multiple tutorials and questions to no avail). I've tried actor and component based, hit and overlap.

The code compiles and runs, but does nothing when collisions occur.

I have the C++ side scroller template project and I'm trying to create a powerup class, which requires collision detection with the player (I'm trying collision with anything atm).

I've got "Simulation generates hit events" and "Generate overlap events" ticked for the sphere collision component, the sphere mesh component, the cube and the player's capsule collider.

PowerUp.h

 // Fill out your copyright notice in the Description page of Project Settings.
 
 #pragma once
 
 #include "GameFramework/Actor.h"
 #include "Engine.h"
 #include "PowerUp.generated.h"
 
 
 UCLASS()
 class SIDESCROLLER_API APowerUp : public AActor
 {
     GENERATED_BODY()
     
 public:    
     // Sets default values for this actor's properties
     APowerUp();
 
     // Called when the game starts or when spawned
     virtual void BeginPlay() override;
     
     // Called when this is hit
     virtual void OnPowerUpHit(AActor *SelfActor, UPrimitiveComponent *OtherActor, FVector NormalImpulse, const FHitResult& Hit);
     virtual void OnPowerUpHitActor(AActor *SelfActor, AActor *OtherActor, FVector NormalImpulse, const FHitResult& Hit);
 
     virtual void OnOverlapBegin(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
 
     // Called every frame
     virtual void Tick( float DeltaSeconds ) override;
 
     //collision primitive to use as root component
     UPROPERTY(EditAnywhere, Category = Pickup)
     USphereComponent *BaseCollisionComponent;
 
     //static mesh to represent the pickup in game
     UPROPERTY(EditInstanceOnly,  BlueprintReadOnly, Category = Pickup)
     UStaticMeshComponent *PickupMesh;
 
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Pickup)
         bool bIsActive;
     
 };

PowerUp.cpp

 #include "SideScroller.h"
 #include "PowerUp.h"
 
 // Sets default values
 APowerUp::APowerUp()
 {
     GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Yellow, TEXT("test"));
     if (BaseCollisionComponent != nullptr){
         BaseCollisionComponent->OnComponentHit.AddDynamic(this, &APowerUp::OnPowerUpHit);
         BaseCollisionComponent->OnComponentBeginOverlap.AddDynamic(this, &APowerUp::OnOverlapBegin);
     }
     OnActorHit.AddDynamic(this, &APowerUp::OnPowerUpHitActor);
     SetActorEnableCollision(true);
      // 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;
 
 }
 
 void APowerUp::OnPowerUpHitActor(AActor *SelfActor, AActor *OtherActor, FVector NormalImpulse, const FHitResult& Hit){
     GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Yellow, TEXT("actor hit!"));
 
 }
 
 // Called when the game starts or when spawned
 void APowerUp::BeginPlay()
 {
     Super::BeginPlay();
     
 }
     
 void APowerUp::OnPowerUpHit(AActor *SelfActor, UPrimitiveComponent *OtherActor, FVector NormalImpulse, const FHitResult& Hit){
     GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Yellow, TEXT("touch !"));
 }
 
 void APowerUp::OnOverlapBegin(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult){
     GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Yellow, TEXT("overlap !"));
 }
 
 // Called every frame
 void APowerUp::Tick( float DeltaTime )
 {
     Super::Tick( DeltaTime );
 
 }



alt text

Product Version: Not Selected
Tags:
more ▼

asked Mar 15 '15 at 12:19 PM in C++ Programming

avatar image

phischphood
21 1 2 8

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

3 answers: sort voted first

Put UFUNCTION() above your Collision Function Prototypes

more ▼

answered Apr 03 '15 at 08:02 AM

avatar image

CHADALAK1
128 25 24 30

avatar image phischphood Apr 07 '15 at 08:45 AM

Thanks, I've only just seen this, I'll try it tomorrow.

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

For me having my binding function in begin play fixed the issue

 GetCapsuleComponent()->OnComponentBeginOverlap.AddDynamic(this, &ATheTowerCharacter::OnOverlapBegin);
more ▼

answered Feb 14 '16 at 07:33 AM

avatar image

hellaeon
26 3 6 8

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

You haven't created your components anywhere. Maybe you are doing it in blueprint (?), but anyway they will be null in your constructor, so your test will fail and you won't add the delegates.

I haven't read on documentation relating to this, but from personal experience I think it's better to register delegates later on in an object's initialization anyway - either PostInitComponents or BeginPlay.

more ▼

answered Apr 08 '15 at 08:33 AM

avatar image

kamrann
2.1k 83 33 119

avatar image phischphood Apr 08 '15 at 09:37 AM

The components are created in the blueprint. I'll try moving the delegates to PostInitComponents tonight (along with CHADALAK1's suggestion)

(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