Having some issues in Unreal engine

Problem no.1:- Door Sound automatically plays once when I press the play button.
Note:- Auto Active is turned off.

CPP:-

// Fill out your copyright notice in the Description page of Project Settings.

#include "DoorMovement.h"
#include "Components/AudioComponent.h"
#include "Components/PrimitiveComponent.h"
#include "Engine/World.h"
#include "GameFramework/Actor.h"
#include "GameFramework/PlayerController.h"

#define OUT

// Sets default values for this component's properties
UDoorMovement::UDoorMovement()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = true;
}

// Called when the game starts
void UDoorMovement::BeginPlay()
{
	Super::BeginPlay();

	// UE_LOG(LogTemp, Warning, TEXT("Name: %s"), *GetOwner()->GetName()) //Debug line to check which-which actor has this component attached

	PressurePlateCheck();
	InitializeBeginning();
	FindSoundComponent();
}

// Called every frame
void UDoorMovement::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	DoorOperations(DeltaTime);
}

void UDoorMovement::OpenDoor(float DeltaTime) // Opens the door
{
	FRotator DoorRotation = GetOwner()->GetActorRotation();
	CurrentYaw = FMath::Lerp(CurrentYaw, OpenAngle, DeltaTime * DoorOpenSpeed);
	DoorRotation.Yaw = CurrentYaw;
	GetOwner()->SetActorRotation(DoorRotation);

	if (!SoundComponent)
	{
		return;
	} // Debug Line

	if (CurrentYaw <= InitialYaw + 10.0f)
	{
		if (PlaySound) // Binary switch to play sound when door gets open
		{
			SoundComponent->Play();
			PlaySound = false;
		}
	}
}

void UDoorMovement::CloseDoor(float DeltaTime) // Closes the door
{
	FRotator DoorRotation = GetOwner()->GetActorRotation();
	CurrentYaw = FMath::Lerp(CurrentYaw, InitialYaw, DeltaTime * DoorCloseSpeed);
	DoorRotation.Yaw = CurrentYaw;
	GetOwner()->SetActorRotation(DoorRotation);

	if (!SoundComponent)
	{
		return;
	} // Debug Line

	if (CurrentYaw <= InitialYaw + 5.0f)
	{
		if (StopSound) // Binary switch to play sound when door gets closed
		{
			SoundComponent->Play();
			StopSound = false;
		}
	}
}

float UDoorMovement::TotalMassFunction() const
{
	float Mass = 0.f;
	TArray<AActor *> OverlappingActors;

	if (!PressurePlate)
	{
		return Mass;
	}

	PressurePlate->GetOverlappingActors(OUT OverlappingActors);

	for (AActor *Actor : OverlappingActors)
	{
		Mass += Actor->FindComponentByClass<UPrimitiveComponent>()->GetMass();
	}

	return Mass;
}

void UDoorMovement::PressurePlateCheck() const
{
	if (!PressurePlate) // This will check the all the actor have this component has volume trigger set
	{
		UE_LOG(LogTemp, Error, TEXT("Please ensure that you initialized trigger volume to %s Actor."), *GetOwner()->GetName());
	}
}

void UDoorMovement::InitializeBeginning()
{
	InitialYaw = GetOwner()->GetActorRotation().Yaw;
	CurrentYaw = InitialYaw;
	OpenAngle = InitialYaw + OpenAngle;

	// ActorThatOpens = GetWorld()->GetFirstPlayerController()->GetPawn(); // Setting the actor which will open the door when entering in trigger volume
}

void UDoorMovement::DoorOperations(float DeltaTime)
{
	if (PressurePlate && TotalMassFunction() >= MassToOpenDoor) // This will exute only when Pressure plate will not be null and declared actor or pawn will be in tigger volume zone
	{
		OpenDoor(DeltaTime);
		DoorOpenTime = GetWorld()->GetTimeSeconds(); // Sets the time of opening door most recently

		StopSound = true; // Resets the binary switch value to default so that sound can be played one again
	}
	else
	{
		if (GetWorld()->GetTimeSeconds() - DoorOpenTime > CloseDoorDelaySeconds) // This line will let close the door only if the delay of given seconds has been made since last opening the door
			CloseDoor(DeltaTime);

		PlaySound = true; // Resets the binary switch value to default so that sound can be played one again
	}
}

void UDoorMovement::FindSoundComponent() // Finds that if audio component exists or not
{
	SoundComponent = GetOwner()->FindComponentByClass<UAudioComponent>();

	if (!SoundComponent)
	{
		UE_LOG(LogTemp, Error, TEXT("Audio component is missing in %s actor."), *GetOwner()->GetName());
		return;
	}
}

Header:-

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Engine/TriggerVolume.h"
#include "DoorMovement.generated.h"

UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class ESCAPEBUILDING2_API UDoorMovement : public UActorComponent
{
	GENERATED_BODY()

public:
	// Sets default values for this component's properties
	UDoorMovement();

	void CloseDoor(float DeltaTime); // This fuction is to close the door
	void DoorOperations(float DeltaTime);
	void FindSoundComponent();
	void InitializeBeginning();
	void OpenDoor(float DeltaTime);	 // This fuction is to open the door
	void PressurePlateCheck() const;
	float TotalMassFunction() const;

	bool PlaySound = true;
	bool StopSound = true;

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

public:
	// Called every frame
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override;

private:
	float InitialYaw;
	float CurrentYaw;

	float DoorOpenTime = 0.f;

	UPROPERTY(EditAnywhere)
	float MassToOpenDoor = 50.f;

	UPROPERTY(EditAnywhere)
	float OpenAngle = 90.f;

	UPROPERTY(EditAnywhere)
	float DoorOpenSpeed = 1.f;

	UPROPERTY(EditAnywhere)
	float DoorCloseSpeed = .5f;

	UPROPERTY(EditAnywhere)
	float CloseDoorDelaySeconds = 2.f;
	
	UPROPERTY(EditAnyWhere)
	ATriggerVolume *PressurePlate = nullptr;

	UPROPERTY()
	UAudioComponent* SoundComponent = nullptr;

	// UPROPERTY(EditAnywhere)
	// AActor *ActorThatOpens = nullptr;

};

Problem No.2:- I have written the code of playing the sound as the sound should play only if while opening the door, door rotation is between initial Yaw and Initial Yaw + 10 degrees, and while closing the door, door rotation is between initial Yaw and Initial Yaw + 10 degrees. But the sound is playing every time I enter and leave the Trigger Volume.

I have copied and pasted that code and it is working as expected. Though the door closing sound is rather late as it’s going to take the door a while to reach InitialYaw + 5.f because as the lerp approaches the target its going to move slower and slower.

Video Link

I have attached the drive link of my project’s recording. As you can see that when I play the game the audio runs automatically. And the sound of the door is not working properly as per my code. If my code is working properly in your project then what could be the reason that it’s not working properly in my project.

Have you tried rebuilding the project by deleting the Binaries and Intermediate folders?

Yes, I tried but the same problem is happening.

In that case would you mind sending me your project using the following link?

https://gdev.tv/projectupload

Please use File > Package Project > Zip Up Project within Unreal as this will ensure only required files are zipped up and things like the Binaries are excluded.

Actually, I am having an issue while Zipping Up The Project because The size of the project even after Zipping Up is 2.6GB. So I am unable to upload it.

The reason for my project the be this large in size is:- I am now working on making a new level as you told me in the last video of the escape building section. So I imported assets to make my level of around 11GB.

All folders of my project:-
Screenshot 2022-04-11 204444

And actually, I also forgot how to remove binary will please remind me just once again.

As mentioned in the steps above, it would already exclude the Binaries if you used Unreal to zip it up. Did you zip it yourself?

I did Zip it by the method you told me to do. But still, its size is 2.6GB.

If you open the zip in something like 7zip you should be able to delete the content within it. The medieval assets can be removed and I can just copy it over on my end if needed.

As you told me, I deleted the Medieval assets, and I filled up the Google form and submitted it.

Above is the link to the Medieval Escape Building Project, there I wrote fresh codes for Opening Doors and Playing Sounds, and because of that, now my door is playing sound at the correct time when it should play. But still, when I press the play button it plays the sound for once in the beginning.

My fresh code for opening door:-

.H File:-

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Engine/TriggerVolume.h"
#include "DoorOpenMedieval.generated.h"


UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class ESCAPEBUILDING2_API UDoorOpenMedieval : public UActorComponent
{
	GENERATED_BODY()

public:	
	// Sets default values for this component's properties
	UDoorOpenMedieval();
	void OpenDoor(float DeltaTime);
	void PressurePlate(float DeltaTime);
	void CloseDoor(float DeltaTime);
	void SettingUp();
protected:
	// Called when the game starts
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

private:

	bool PlaySound = true;
	bool CloseSound = true;

	float InitialYaw = 0.f;

	UPROPERTY()
	APawn* PawnThatOpens = nullptr;

	UPROPERTY(EditAnywhere)
	float DoorRotationValue = 90.f;

	UPROPERTY(EditAnywhere)
	float DoorOpenTime = 0.5f;

	UPROPERTY(EditAnywhere)
	float DoorCloseTime = 0.5f;

	UPROPERTY(EditAnywhere)
	ATriggerVolume* TriggerVolume = nullptr;

	UPROPERTY(EditAnywhere)
	AActor* ActorThatOpens = nullptr;

	UPROPERTY(EditAnywhere)
	AActor* ActorThatCloses = nullptr;

	UPROPERTY()
	UAudioComponent* SoundComponent = nullptr;
};

CPP File:-

// Fill out your copyright notice in the Description page of Project Settings.

#include "DoorOpenMedieval.h"
#include "Components/AudioComponent.h"
#include "GameFramework/Actor.h"
#include "GameFramework/Pawn.h"
#include "GameFramework/PlayerController.h"
#include "Engine/World.h"

// Sets default values for this component's properties
UDoorOpenMedieval::UDoorOpenMedieval()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = true;

	// ...
}

// Called when the game starts
void UDoorOpenMedieval::BeginPlay()
{
	Super::BeginPlay();
	SettingUp();
	// ...
}

// Called every frame
void UDoorOpenMedieval::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	PressurePlate(DeltaTime);
}

void UDoorOpenMedieval::OpenDoor(float DeltaTime)
{
	FRotator DoorRotation = GetOwner()->GetActorRotation();
	float CurrentYaw = DoorRotation.Yaw;
	CurrentYaw = FMath::Lerp(CurrentYaw, DoorRotationValue, DeltaTime * DoorOpenTime);
	DoorRotation.Yaw = CurrentYaw;
	GetOwner()->SetActorRotation(DoorRotation);

	if (!SoundComponent)
	{
		return;
	}

	if (CurrentYaw <= InitialYaw + 10.0f)
	{
		if (PlaySound)
		{
			SoundComponent->Play();
			PlaySound = false;
		}
	}
}

void UDoorOpenMedieval::CloseDoor(float DeltaTime)
{
	FRotator DoorRotation = GetOwner()->GetActorRotation();
	float CurrentYaw = DoorRotation.Yaw;
	CurrentYaw = FMath::Lerp(CurrentYaw, InitialYaw, DeltaTime * DoorCloseTime);
	DoorRotation.Yaw = CurrentYaw;
	GetOwner()->SetActorRotation(DoorRotation);

	if (!SoundComponent)
	{
		return;
	}

	if (CurrentYaw <= InitialYaw + 10.0f)
	{
		if (CloseSound)
		{
			SoundComponent->Play();
			CloseSound = false;
		}
	}
}

void UDoorOpenMedieval::PressurePlate(float DeltaTime)
{
	if (!TriggerVolume)
	{
		UE_LOG(LogTemp, Error, TEXT("Trigger Volume not found in %s actor."), *GetOwner()->GetName());
		return;
	}

	if (ActorThatCloses && TriggerVolume->IsOverlappingActor(ActorThatCloses))
	{
		CloseDoor(DeltaTime);
		PlaySound = true;
	}
	else if (ActorThatCloses && !(TriggerVolume->IsOverlappingActor(ActorThatCloses)))
	{
		OpenDoor(DeltaTime);
		CloseSound = true;
	}
	else if (ActorThatOpens && TriggerVolume->IsOverlappingActor(ActorThatOpens))
	{
		OpenDoor(DeltaTime);
		CloseSound = true;
	}
	else if (!ActorThatOpens && PawnThatOpens && TriggerVolume->IsOverlappingActor(PawnThatOpens))
	{
		OpenDoor(DeltaTime);
		CloseSound = true;
	}
	else
	{
		CloseDoor(DeltaTime);
		PlaySound = true;
	}
}

void UDoorOpenMedieval::SettingUp()
{
	InitialYaw = GetOwner()->GetActorRotation().Yaw;

	PawnThatOpens = GetWorld()->GetFirstPlayerController()->GetPawn();

	SoundComponent = GetOwner()->FindComponentByClass<UAudioComponent>();
	if (!SoundComponent)
	{
		return;
	}
}

And yes there is no Comments because I thought that I am the only one whose gonna read this code but now I regret. But still I thinks that You will understand the code where I played the sound. And in the PressurePlate function I used this many Conditional loops cause I used the same component on all the doors in the level and the condition for opening for all the doors are different.

I adding logs to both to see what’s playing. The log showed that CloseDoor played as soon as the game began. So a quick look at the header showed that your StopSound was initially true which results in it being played when the game begins.

Thanks a lot I resolved the issue and everything is working as expected. Honestly, I didn’t catch the issue even after reading this reply of your but it gave me a hint, and then I thought a lot, and finally, now I am able to understand what you want to tell me. All credit goes to you. :smiley:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms