
Unreal Engine Blueprint – Rotate an object using a key (Part 2 of 2)
This post is part 2 of a previous post on rotating a cube (or any object) without an event tick, but this time we will use a key input to stop or start an object from rotating.
In the same blueprint that was created in part 1 go into the Event Graph and follow the steps below.
Enable player controller so the object rotation can be enabled or disabled with a key.
- In the component section add a ‘box collision’.
- Scale this to the required size and make sure your character is able to enter this box.
- The other components required have already been added in part 1.
Components:
- Make sure the RotatingMovement component has Auto Activate unticked.
- To do this select the RotatingMovement component and in its details panel (right hand side) search for ‘Auto Activate’ and untick the box.
- If you don’t the object will just rotate from the start of the game or if you want to rotate from the start – keep this box ticked.
In the Event Graph:
- Search for ‘Get Player Controller’
- Drag off from this node – from the ‘Return value’ and Search for and add ‘Enable input’.
- Repeat step above but this time search for and add ‘Disable input’.
- Make sure the ‘Return value’ is plugged into ‘Player Controller’ and not ‘Target’ (see image below).
- Select the Box collision.
- On the right hand side details panel scroll down and select ‘On Component Begin Overlap’
- Pull off the top socket execution node (white arrow) and plug into the ‘Enable input’
- Repeat step above and select ‘On Component End Overlap’
- Pull off the top socket execution node (white arrow) and plug into the ‘Disable input’
Now the player controller will be activated when the player enters the collision box.
Adding the Key Input:
- Right click and search for the E key (or use whatever key you want to use as it doesn’t have to be the E key).
- From the E key drag off from ‘Pressed’ and search for Branch.
- Add a branch > Where it says condition right click and select promote to a variable.
- Rename variable to Activate (this is a Boolean) and we are adding this so the branch knows what path to follow).Â
- The E key will only connect to one node so it needs some logic to function like a switch.
- Press compile and make sure the Activate value is ticked in its details panel on the right hand side (see image below).
- Drag off from True and search for Activate (with RotatingMovement).
- When selected it will auto add the Rotatingmovement component into the event graph.
- From the Rotatingmovement component drag off and search for Deactivate (with RotatingMovement).
Setting the Boolean:
- Finally, from the variables panel on the right hand side there is the Boolean variable that we called activate.
- Drag off this varaible and place after activate > select Set and then plug in the Activate node.
- Repeat step above but this time tick the Activate box.Â
The reason we have done this is so when pressing E (inside the collision box) it will first follow the True Activate Branch and its final state will be to deactivate the switch (as we have not ticked the box).
When the E button is pressed again, now it will follow the false branch (as it was was deactivated or set to false) and it will follow the path to the Deactivate node and the rotation will stop. Finally it will be re-activated as the last Boolean is ticked. This logic is also used for light switches or turning on or off a music player.
Remember to scale the collision box to the appropriate size so your character can enter it and press the E key to activate the rotation. Below I swapped out the cube for a large metal sculpture of the world.Â
This blueprint is now ready to work in the level and you can place it where ever it is needed. For further tutorials on blueprints see the tutorials and tips page or if new to blueprints have a look at the Introduction to blueprints page.
If you have any questions or got stuck please do leave a comment in the comments section.Â