Open door via rotateactor or animation?

Quick question regarding some basic C++ in UE4. I currently have a working door, walk up and press E and it snaps open 90degrees, press E again and it snaps closed 90degrees. This also plays an open and closed door sound accordingly. The problem is currently I’m just using the function SetActorRotation to open and close the door. Could anyone point me into the right direction on a better way to handle this? Should I be using an animation on the door or should I be animating it in the code by using a lerp or something? If I should be animating through the code is their any suggestions on how to do this? (I am new to UE4, coming over from Unity C#).

Included some pseudo code below to show how my door is currently working.

Thanks,
Mark

pseudo code:



class MyGameCharacter
	Interactable CurrentInteractable;
	CheckForInteractables()	// determines if interactable object is in range (via linetrace), if so add to CurrentInteractable, otherwise assign nullptr. Function is called inside Tick()
	Interact() // called when the key E is pressed, if CurrentInteractable != nullptr, call CurrentInteractable->Interact()

class Intractable
	function Interact();

class Door (inherits Interactable)
	bool bOpened;
	bool bLocked;
	UAudioComponent* door_open;
	UAudioComponent* door_close;
	UAudioComponent* door_locked;

	function Interact() {
		if(!bLocked)
		{
			if(!bOpened)
			{
				// play open animition OR rotate actor yaw by 90degrees?
				playsound door_open;
				bOpened = true;
			}
			else
			{
				// play close animition OR rotate actor yaw by -90degrees?
				playsound door_close;
				bOpened = false;
			}
		}
		else
		{
			play door_locked;
		}
	}


A pretty great way to do something like this is to us a timeline in blueprints.



UFUNCTION(BlueprintImplementableEvent)) void OpenDoor();
UFUNCTION(BlueprintImplementableEvent)) void CloseDoor();


If you put those 2 functions in your .h, you can then access them from the blueprint as events.

This means in your code, simply call OpenDoor() when it should open, etc.

Then in it’s blueprint you can create an OpenDoor event that will be triggered when the C++ version is called.
You can then attach a timeline to that event which will let you make a really nice open or close animation to your door. :slight_smile:
(Timelines are only avilable in blueprints)

Timeline Tutorial

I have very little experience in blueprints so I have a follow up question. If I go this route does that mean instead of dropping a the C++ class Door into my level I would right click and create a blueprint inheriting from Door then drag and drop that blueprint into my level instead?

Yes exactly.
And I’d actually advise never putting a C++ class directly onto the stage (you’ll start to see the benefits eventually)

  • Animation goes from 0 to 90 in 0.5 seconds
  • Edit the curve however you like

Set it for the actual mesh, so you can set the actor rotation, and then the door will turn 90 degrees within the actor.