Rotate an Actor with Code

Hello All,

During this whole lecture I was expecting that at the end we would have a door that would actually open as that’s what he was demonstrating. When I got near the end of the lecture, he used the line SetRotation, I thought to myself, oh cool, Unreal is going to automate the process of the animation to open the door to the correct angle - wow this is so cool. Then I ran the code and saw that the door was just open, no animation, now I was thinking, “oh hell what have I done wrong the code isn’t working properly”. Wathced the video a couple of times looked over the code - couldn’t see anything wrong - time to send another message into ASK forum again asking for help - @DanM must be getting sick of me by now. Then I listened to a comment right near the end of the lecture and heard him say, the door snaps open. Damn no animation at all.

That’s not good enough for me, went and had lunch and had time to think about what I needed to do, no looking at Documentation because the Unreal docs are some of the worst documentation I have ever had the privilege to read. I could see in the code that there was a space for something to happen every single frame, then thought that’s the answer have some code in there that would run for a specific amount of time and then stop, aha have the current position the target position then increment by 1f every frame and the setRotation at that point and opened it 110 degrees so not just 90. Had to jiggle the code made some mistakes along the way one major, and it after I got it sorted worked on both doors. But, there had to be a but in there the door to outside was opening a different way with the code, it opened inside when snapping open with the code on every frame opened outside. Can’t figure that out anyway, I was pretty chuffed with myself having a smooth opening door.

Cool - now with the removal of the collisions I have an opening door, that leads outside. Now, Next Challenge, set it up a collision detector to open when I get near the door!?!

Cheers.

1 Like

Hi Vaughan.
It’s cool that you figured this out. The lecture I think you’re talking about is really to demonstrate the changes applied to actors via code. The following lectures actually deal with the animation of the rotation.

There’s a way of dealing with this by using AddActorLocalRotation. This uses an FRotator to apply a change in Rotation to the current angle of rotation. In the case of a door, you’re affecting the Rotation about Z or the Yaw and may choose to apply .5 degrees per tick which would take 3 seconds at 60FPS to open by 90 degrees. You can then capture the initial rotation in BeginPlay and apply a small rotation every tick which would be something like DegreesPerSecond * DeltaTime, so in the case above, you want 30 degrees per second, OR, you could specifiy open speed in seconds and open angle in degrees and do (OpenAngle/OpenSpeed) * DeltaTime which then works for a varying FPS. You can then check the new rotation and see if the OpenAngle is the correct amount of Degrees and not rotate any more. Having the initial yaw also enables closing, again dealt with later.

This method is covered in the course, the next few lectures if I recall. There’s a lot of complications such as the door being positioned so that the start angle is not 0.

Have a look here. There’s not a lot of information to be honest.
AddActorLocalRotation | Unreal Engine Documentation

1 Like

Privacy & Terms