Announcement

Collapse
No announcement yet.

Can't Get to Control Which Type of Class to Trigger an Overlap Event

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Can't Get to Control Which Type of Class to Trigger an Overlap Event

    Hello again, another newbie problem by me!

    I was trying to make a very simple game where the player tries to get across the obstacles so as to get to the finishing box. When it enters to this box, a specific sound is to play.

    Problem is, when the other actors enters to finishing box, the sound playing like it was the player who enters the box.How can I edit my code to prevent this and make sure it plays only by the player entering the zone?

    .h file
    Code:
    // Fill out your copyright notice in the Description page of Project Settings.
    
    #pragma once
    
    #include "CoreMinimal.h"
    #include "GameFramework/Actor.h"
    #include "Components/ShapeComponent.h"
    #include "Components/BoxComponent.h"
    #include "Runtime/Engine/Classes/Kismet/GameplayStatics.h"
    #include "Runtime/Engine/Classes/GameFramework/Character.h"
    #include "FinishLine.generated.h"
    
    UCLASS()
    class TUT_FPSHORROR_API AFinishLine : public AActor
    {
        GENERATED_BODY()
    
    public:    
        // Sets default values for this actor's properties
        AFinishLine();
    
        UPROPERTY(EditAnywhere)
            UShapeComponent* SC_FinishLineBox;
    
        UPROPERTY(EditAnywhere)
            USoundBase* SB_Victory;
    
        UFUNCTION()
            //Wrong Usage!
            //void TriggerEnter(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
        // update the parameters for TriggerEnter in both the .h and .cpp
            void TriggerEnter(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
    
    protected:
        // Called when the game starts or when spawned
        virtual void BeginPlay() override;
    
    public:    
        // Called every frame
        virtual void Tick(float DeltaTime) override;
    
    
    
    };
    cpp file
    Code:
    // Fill out your copyright notice in the Description page of Project Settings.
    
    #pragma once
    
    #include "CoreMinimal.h"
    #include "GameFramework/Actor.h"
    #include "Components/ShapeComponent.h"
    #include "Components/BoxComponent.h"
    #include "Runtime/Engine/Classes/Kismet/GameplayStatics.h"
    #include "Runtime/Engine/Classes/GameFramework/Character.h"
    #include "FinishLine.generated.h"
    
    UCLASS()
    class TUT_FPSHORROR_API AFinishLine : public AActor
    {
        GENERATED_BODY()
    
    public:    
        // Sets default values for this actor's properties
        AFinishLine();
    
        UPROPERTY(EditAnywhere)
            UShapeComponent* SC_FinishLineBox;
    
        UPROPERTY(EditAnywhere)
            USoundBase* SB_Victory;
    
        UFUNCTION()
            //Wrong Usage!
            //void TriggerEnter(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
        // update the parameters for TriggerEnter in both the .h and .cpp
            void TriggerEnter(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
    
    protected:
        // Called when the game starts or when spawned
        virtual void BeginPlay() override;
    
    public:    
        // Called every frame
        virtual void Tick(float DeltaTime) override;
    
    
    
    };

    #2
    Why not just check if the pawn is your character... for example:

    Code:
     void AFinishLine::TriggerEnter(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
    {    
        if (OtherActor == nullptr || Cast<MyPawnClass>(OtherActor) == null) return;  
        // Do your code here.  
    }
    Last edited by WisE DRL; 09-27-2018, 08:26 AM.
    Joseph Wilcox
    [Technical Director] WisEngineering
    https://www.wisedrl.com

    Comment


      #3
      Originally posted by Joe Wilcox WisE View Post
      Why not just check if the pawn is your character... for example:

      Code:
      void AFinishLine::TriggerEnter(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
      {
      if (OtherActor == nullptr || Cast<MyPawnClass>(OwnerActor) == null) return;
      // Do your code here.
      }
      In addition to above solution you can configure your collision component (SC_FinishLine in this case) to only react with Pawn collision channel (assuming your players are Pawn delivered Actors). Doing that will limit the TriggerEnter function calls. I do not have access to UE code currently but something along these lines:
      Code:
      AFinishLine::AFinishLine()
      {
         ... Initialization of components etc.
         SC_FinishLineBox->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore); // Set all other response channels to ignore
         SC_FinishLineBox->SetCollisionResponceToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap); // Set Pawn collision channel to Overlap
      }
      Check out documentation of UPrimitiveComponent and the SetCollision... functions for more info.

      Comment


        #4
        Originally posted by Joe Wilcox WisE View Post
        Why not just check if the pawn is your character... for example:

        Code:
        void AFinishLine::TriggerEnter(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
        {
        if (OtherActor == nullptr || Cast<MyPawnClass>(OwnerActor) == null) return;
        // Do your code here.
        }
        Thanks for the help, but I don't know how to tell C++ to which pawns to look for (the obstacles are set up to be the actors, actually). Also, I don't know how to write the right code to tell C++ that "you need to search for the 'OwnerActor' either. Can you please help, or redirect me to some tutorials on that matter?
        Originally posted by kreivi_krapula View Post

        In addition to above solution you can configure your collision component (SC_FinishLine in this case) to only react with Pawn collision channel (assuming your players are Pawn delivered Actors). Doing that will limit the TriggerEnter function calls. I do not have access to UE code currently but something along these lines:
        Code:
        AFinishLine::AFinishLine()
        {
        ... Initialization of components etc.
        SC_FinishLineBox->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore); // Set all other response channels to ignore
        SC_FinishLineBox->SetCollisionResponceToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap); // Set Pawn collision channel to Overlap
        }
        Check out documentation of UPrimitiveComponent and the SetCollision... functions for more info.
        I did what you said, but didn't work man. But appreciated for your help anyways!

        Comment


          #5
          Sorry about that. OwnerActor is a typo. I fixed the original example. This part: Cast<MyPawnClass>(OtherActor) == null is how you do the check. What I'm doing there is taking OtherActor and looking to see if it's MyPawnClass. If it's not, just exit. Kreivi's solution sort of does the same thing at a deeper level and is a better solution if you are looking to just only track pawn collision, not a specific pawn type. It causes the collision system to ignore anything not a pawn.

          Joseph Wilcox
          [Technical Director] WisEngineering
          https://www.wisedrl.com

          Comment


            #6
            BTW, to make this simpler derive your custom class from Unreal "TriggerVolume" class ...

            Comment

            Working...
            X