Hi guys the thing is that I’m trying to open and close a door automatically with a box trigger and there’s no problem when I write a debug message on the Exit and enter overlap, I even can open the door but when I add the close and open door action my game frozes… I don’t know whats the error. … Here’s my code
//--> .h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/Actor.h"
#include "PuertaComportamiento.generated.h"
UCLASS()
class SQUASH_API APuertaComportamiento : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
APuertaComportamiento();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick(float DeltaSeconds) override;
void openDoor(bool open);
float RunningTime;
UPROPERTY(EditAnywhere)
UShapeComponent * tbox;
UPROPERTY(EditAnywhere)
UStaticMeshComponent * door;
UFUNCTION()
void TriggerEnter(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
UFUNCTION()
void TriggerExit(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
};
//--> cpp
Constructor
// Sets default values
APuertaComportamiento::APuertaComportamiento()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;
tbox = CreateDefaultSubobject<UBoxComponent>(TEXT("BoxTrigger"));
FVector tboxSize = FVector(1.0f, 1.5f, 1.0f);
tbox->SetRelativeScale3D(tboxSize);
tbox->bGenerateOverlapEvents = true;
RootComponent = tbox;
door = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("door"));
door->AttachTo(RootComponent);
//check(InputComponent);
tbox->OnComponentBeginOverlap.AddDynamic(this, &APuertaComportamiento::TriggerEnter);
tbox->OnComponentEndOverlap.AddDynamic(this, &APuertaComportamiento::TriggerExit);
}
// trigger enter and trigger exit
void APuertaComportamiento::TriggerEnter(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) {
//FVector jugadorPos = OtherActor->GetActorLocation();
if (OtherActor && (OtherActor != this) && OtherComp)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT(" im on triggerEnter"));
openDoor(true);
}
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("im out triggerEnter"));
}
void APuertaComportamiento::TriggerExit(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) {
if (OtherActor && (OtherActor != this) && OtherComp)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("im on triggerExit"));
openDoor(false);
}
}
// open door
void APuertaComportamiento : penDoor(bool open)
{
float rotZ = (open ? 120 : 0);
FRotator rotator = this->GetActorRotation();
rotator.Yaw = rotZ;
SetActorRotation(rotator);
}