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"

Do OnComponentOverlap Events work with static meshes ?

Because I'm trying to create a overlap event in code and it won't work with a UstaticMeshComponent attatched. I used the exact same code for a different actor but had a collision capsule instead. What has changed that stops the collision from occurring ?

Code:

Brick.cpp

 Brick.cpp
 
 
 #include "Mario3D.h"
 #include "Brick.h"
 #include "Coin.h"
 #include "Engine.h"
 
 
 ABrick::ABrick(const class FPostConstructInitializeProperties& PCIP)
     : Super(PCIP)
 {
     //Brick Mesh
     static ConstructorHelpers::FObjectFinder<UStaticMesh> Mesh(TEXT("StaticMesh'/Game/Shapes/Shape_Cube.Shape_Cube'"));
     BrickMesh = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("BrickMeshComponent"));
     BrickMesh->SetStaticMesh(Mesh.Object);
     BrickMesh->SetWorldScale3D(FVector(0.64, 0.64, 0.64));
     RootComponent = CollisionComp;
 
     // Creates Collision Component
     CollisionComp = PCIP.CreateDefaultSubobject<UBoxComponent>(this, TEXT("CollisionComponent"));
     CollisionComp->OnComponentBeginOverlap.AddDynamic(this, &ABrick::OnOverlap);
     CollisionComp->SetBoxExtent(FVector(32, 32, 1));
     CollisionComp->bGenerateOverlapEvents = true;
     UPrimitiveComponent::SetNotifyRigidBodyCollision(true);
 }
 
 void ABrick::OnOverlap(class AActor * OtherActor, class UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
 {
     if (OtherActor && (OtherActor != this) && OtherComp)
     {
         // Spawn Actor & Play Sound
         ACoin* SpawnedActor2 = GetWorld()->SpawnActor<ACoin>(GetActorLocation() + FVector(0, 0, 300), FRotator(0, 0, 0));;
     }
 }
 

Brick.h

 #pragma once
 
 #include "GameFramework/Actor.h"
 #include "Brick.generated.h"
 
 /**
  * 
  */
 UCLASS()
 class MARIO3D_API ABrick : public AActor
 {
     GENERATED_UCLASS_BODY()
         UPROPERTY(VisibleAnywhere, Category = Brick)
         TSubobjectPtr<UBoxComponent> CollisionComp;
 
     UPROPERTY(VisibleAnywhere, Category = Brick)
         TSubobjectPtr<UStaticMeshComponent> BrickMesh;
     
     UFUNCTION()
         void OnOverlap(class AActor * OtherActor, class UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult);
 };
 
 Coin.h
 
 
 
 #pragma once
 
 #include "GameFramework/Actor.h"
 #include "CoinSound.h"
 #include "Coin.generated.h"
 
 
 /**
  * 
  */
 UCLASS()
 class MARIO3D_API ACoin : public AActor
 {
     GENERATED_UCLASS_BODY()
 
         /*Declares Collision Component*/
         UPROPERTY(VisibleDefaultsOnly, Category = Coin)
         TSubobjectPtr<USphereComponent> CollisionComponent;
 
     UPROPERTY(VisibleDefaultsOnly, Category = Coin)
         float RotationRate;
 
     UPROPERTY(VisibleDefaultsOnly, Category = CoinSound)
         TSubclassOf<ACoinSound> ASound;
 
     virtual void Tick(float DeltaTime) override;
 
     /** called when coin hits something or vice versa */
     UFUNCTION()
         void OnOverlap(class AActor * OtherActor, class UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult);
     
 };
 

Product Version: Not Selected
Tags:
more ▼

asked Sep 07 '14 at 02:29 PM in C++ Programming

avatar image

Green_
57 27 18 20

(comments are locked)
10|2000 characters needed characters left

2 answers: sort voted first

If you set up a static mesh with blueprint, there is a checkbox named something like "generate overlap events". You're doing this with the code but should be same thing, I assume you should enable this to get it working. Collision capsule is a component meant to be used for generating events like touch, overlap etc, so either make the StaticMesh generate overlap events or just add a collision capsule to your code. I'm sure if you try to do this with blueprint it will work, I haven't tried this with the code yet thou.

more ▼

answered Sep 08 '14 at 12:55 AM

avatar image

Jurif
632 25 15 44

avatar image Green_ Sep 08 '14 at 01:53 AM

It's a USphereComponent sorry, not sure if it makes a difference, i've added the code.

avatar image ChenChen Jul 22 '16 at 08:30 AM

Have u solve this problem?

avatar image ChenChen Jul 22 '16 at 08:44 AM

I solve it .

(comments are locked)
10|2000 characters needed characters left

u do not set the collisionResponseToChannle .

more ▼

answered Jul 22 '16 at 10:44 AM

avatar image

ChenChen
1 1

(comments are locked)
10|2000 characters needed characters left
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