Need some assistance - StaticMesh change with asset

I have a door I’m trying to use the OpenDoor component on a door I got in the marketplace.
It works fine on the full door except it’s rotation point is in the middle. However, it has two meshes to act as left/right doors in the actor as shown. I’ve tried to grab that specific mesh to rotate it and it just rotates the main door still.

Here’s the code, any help on how to finagle this so only the left door opens is appreciated.
I modified the OpenDoor component to use the Metal_Left_Door if it’s available and assign it to a UStaticMesh pointer, then I have an OpenLeftDoor function that opens that door if it’s set. I would add that the right component gets set as LeftDoor, but when I get that component rotation, it still just swings the main actor like I didn’t put this code in at all.

TArray<UStaticMeshComponent*> components;
		GetOwner()->GetComponents(components);
		
		for (int32 i = 0; i < components.Num(); i++)
		{
			UE_LOG(LogTemp, Warning, TEXT("%i: component is included on this object, mesh name %s."), i, *components[i]->GetName());
			
			if(components[i]->GetName().Equals("Metal_Door_Left"))
			{
				LeftDoor = components[i];
				UE_LOG(LogTemp, Warning, TEXT("LeftDoor has been assigned to %s."), *LeftDoor->GetName())
				//LeftDoor->AttachToComponent(); 	
			}
			 
		}

Full OpenDoor code.

OpenDoor.cpp

// Copyright Douglas Taggart 5/2020

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

// Sets default values for this component's properties
UOpenDoor::UOpenDoor()
{
	// 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 UOpenDoor::BeginPlay()
{
	Super::BeginPlay();

	if(!UseLeftDoor)
	{
		InitialYaw = GetOwner()->GetActorRotation().Yaw;
		
		
	} else
	{
		
		// get the right components to use for LeftDoor
		TArray<UStaticMeshComponent*> components;
		GetOwner()->GetComponents(components);
		
		for (int32 i = 0; i < components.Num(); i++)
		{
			UE_LOG(LogTemp, Warning, TEXT("%i: component is included on this object, mesh name %s."), i, *components[i]->GetName());
			
			if(components[i]->GetName().Equals("Metal_Door_Left"))
			{
				LeftDoor = components[i];
				UE_LOG(LogTemp, Warning, TEXT("LeftDoor has been assigned to %s."), *LeftDoor->GetName())
				//LeftDoor->AttachToComponent(); 	
			}
			 
		}
		
		InitialYaw = LeftDoor->GetComponentRotation().Yaw;

		// AActor* LeftDoor = 
			
	}
	
	CurrentYaw = InitialYaw;
	TargetYaw = InitialYaw + TargetYaw;
	

	CheckPressurePlateSet();

	FindAudioComponent();
}

void UOpenDoor::CheckPressurePlateSet() const
{
	if(!PressurePlate)
	{
		UE_LOG(LogTemp, Error, TEXT("%s has the OpenDoor component on it, but no PressurePlate set."), *GetOwner()->GetName());
	}
}

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

	if (TotalMassOfActors() > MassRequiredToOpenDoor)
	{
		if(LeftDoor)
		{
			OpenLeftDoor(DeltaTime);
		} 
		else 
		{
			OpenDoor(DeltaTime);
		}
		DoorLastOpened = GetWorld()->GetTimeSeconds();
	}
	else
	{
		if ((GetWorld()->GetTimeSeconds() - DoorLastOpened) >= DoorCloseDelay)	
		{
			if(LeftDoor)
			{
				CloseLeftDoor(DeltaTime);
			}
			else
			{
				CloseDoor(DeltaTime);
			}
			
		}
	}
}

void UOpenDoor::OpenDoor(float DeltaTime)
{
	FRotator CurrentRotation = GetOwner()->GetActorRotation();
	CurrentRotation.Yaw = FMath::FInterpConstantTo(CurrentRotation.Yaw, TargetYaw, DeltaTime, DoorOpenSpeed);
	GetOwner()->SetActorRotation(CurrentRotation);

	CloseDoorSound = false;
	if(!AudioComponent){return;}
	if (!OpenDoorSound)
	{
		AudioComponent->Play();
		OpenDoorSound = true;
	}
}

void UOpenDoor::OpenLeftDoor(float DeltaTime)
{
	FRotator CurrentRotation = LeftDoor->GetComponentRotation();
	UE_LOG(LogTemp, Warning, TEXT("I'm in OpenLeftDoor executing."));
	CurrentRotation.Yaw = FMath::FInterpConstantTo(CurrentRotation.Yaw, TargetYaw, DeltaTime, DoorOpenSpeed);
	LeftDoor->SetWorldRotation(CurrentRotation);

	CloseDoorSound = false;
	if(!AudioComponent){return;}
	if (!OpenDoorSound)
	{
		AudioComponent->Play();
		OpenDoorSound = true;
	}
}

void UOpenDoor::CloseDoor(float DeltaTime)
{
	FRotator CurrentRotation = GetOwner()->GetActorRotation();
	CurrentRotation.Yaw = FMath::FInterpConstantTo(CurrentRotation.Yaw, InitialYaw, DeltaTime, DoorOpenSpeed);
	GetOwner()->SetActorRotation(CurrentRotation);
	
	OpenDoorSound = false;
	if (!AudioComponent){return;}
	if (!CloseDoorSound)
	{
		AudioComponent->Play();
		CloseDoorSound = true;
	}
}

void UOpenDoor::CloseLeftDoor(float DeltaTime)
{
	FRotator CurrentRotation = LeftDoor->GetComponentRotation();
	CurrentRotation.Yaw = FMath::FInterpConstantTo(CurrentRotation.Yaw, InitialYaw, DeltaTime, DoorOpenSpeed);
	LeftDoor->SetWorldRotation(CurrentRotation);
	// GetOwner()->SetActorRotation(CurrentRotation);
	
	OpenDoorSound = false;
	if (!AudioComponent){return;}
	if (!CloseDoorSound)
	{
		AudioComponent->Play();
		CloseDoorSound = true;
	}
}

void UOpenDoor::FindAudioComponent()
{
	AudioComponent = GetOwner()->FindComponentByClass<UAudioComponent>();

	if(!AudioComponent)
	{
		UE_LOG(LogTemp, Error, TEXT("%s missing audio component!"), *GetOwner()->GetName());
	}
}

float UOpenDoor::TotalMassOfActors() const
{
	float TotalMass = 0.f;

	//  Find all overlapping actors
	TArray<AActor*> OverlappingActors;
	
	if(!PressurePlate){return TotalMass;}
	PressurePlate->GetOverlappingActors(OverlappingActors);

	for (AActor* Actor : OverlappingActors)
	{
		TotalMass += Actor->FindComponentByClass<UPrimitiveComponent>()->GetMass();
	}
	// Add up their masses.

	return TotalMass;
}

OpenDoor.h

// Copyright Douglas Taggart 5/2020

#pragma once

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


UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class BUILDINGESCAPE_API UOpenDoor : public UActorComponent
{
	GENERATED_BODY()

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

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

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

	void OpenLeftDoor(float DeltaTime);
	void CloseLeftDoor(float DeltaTime);
	
	// Tracks whether sound has been played
	bool OpenDoorSound = false;
	bool CloseDoorSound = true;

private:

	void CheckPressurePlateSet() const;

	float InitialYaw;
	float CurrentYaw;
	float DoorLastOpened = 0.f;
	
	UStaticMeshComponent* LeftDoor = nullptr;
	
	UPROPERTY(EditAnywhere)
	float TargetYaw = -90.f;
	UPROPERTY(EditAnywhere)
	float DoorOpenSpeed = 10.0f;
	UPROPERTY(EditAnywhere)
	ATriggerVolume* PressurePlate = nullptr;
	UPROPERTY(EditAnywhere)
	float DoorCloseDelay = 2.f;
	UPROPERTY(EditAnywhere)
	float MassRequiredToOpenDoor = 50.f;
	UPROPERTY(EditAnywhere)
	bool UseLeftDoor = false;
	UPROPERTY(EditAnywhere)
	UAudioComponent* AudioComponent = nullptr;
};

fixed a minor typo in the UE_LOG within OpenLeftDoor to it should compile.

Well this sounds like you could use AddRelativeRotation or SetRelativeRotation for the component which is the door you want to rotate. With this you wouldn’t need to store the initial rotation as it can work off the relative rotation from the root component.

I have a feeling now that I’ve started the Battle Tank (original), that this will likely get explained just based on the fact you have to move the turret and tracks (assumed). I’ve only gotten as far as putting the tank model together, super excited about that section. Thanks for hints though. I had subsequently tried to just SetRelativeRotation on my left door, and use the previous animation from OpenDoor.cpp on it… BUT I had some odd results. It looked like the door was opening and closing so fast and animating weird. When I got the pawn close to the door in play, it blasted my pawn nearly out of the map. It was quite amusing actually. Anyway thanks.

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

Privacy & Terms