For the first, excuse me for my bad English, I would tell about this.
In many games (especially in MMORPG) in Main Menu there is a “Create Character Screen” where a player creates his character. And this character rotates for Y-axis. If a player moves a mouse cursor with a left mouse button pressed around an object, it rotates according a mouse cursor. This case a player can see an object’s side he wants.
In this tutorial I tell about how to make a rotatable object in the 3d scene. In my example this object is rotated around Y-axis (Yaw), but you can make it’s rotation around X or Z axis. You should know an object rotation doesn’t refer to UMG.
Next pic I show a full Blueprint func:
Ok, let’s begin to dig in this program step by step. The first thing we need an event executes when a mouse is clicked by an user. As you know in UE4 every objects has an OnPressed event. But we will program a Level Blueprint graph. This is useful for us because we can control every object in the scene. But in this way we don’t have an OnPressed event. So we can add this very simply. Open up Project Settings (Edit -> Project Settings), in this menu click an Input item on the left side, then click on the plus button next to an Action Mapping item on the right. Then expand this item. You will see a text field, you should input an event name. In my case this called MouseClick. Under a text field you see a combo box which contains events of input devices. Then you’d expand a Mouse item and choose a Left Mouse Button event. Next pic you will see my case:
Now we can return to Level Blueprint and create this new event from a right mouse button menu.
As you can see this block has 2 outputs: when Pressed occurs and when Released occurs. We need a Released event. So when does Pressed occur? This event should occur when a player clicks on a rotatable object. So for the first time after an import a 3d model we need to make a class for this model. To begin we need to create a new Blueprint class which inherited of an Actor parent class, after that, double click on it and an editor windows will open. Switch to Components inset and drag a 3d model from Content Browser to this window on Components panel. Well, rename a new object and Blueprint class. You can apply some materials to this model. Next you should switch to Graph inset. Firstly we should add new variable of bool type. I called this “canRotate” cause this variable will allow or not to a user rotate an object. This variable will connect to our new sub-Actor class. Then right click on Graph space and choose Event Actor on Clicked from the context menu. This event will occur when a player press our object. OK, next drag and drop a canRotate variable from a panel to a Graph space. A context menu will appear, choose Set item from this. A new block will appear. Check canRotate and connect this block with an Event Actor on Clicked event.
Note you should click on an eye next to canRotate variable in the panel. It will be yellow. This action makes public variables. And the code from another Blueprint can use these variables.
We should return to Level Blueprint again. Add two variables: “delta” of float type and “tank” of reference to new created class 3d object.
In this graph we already have one block – InputAction MouseClick event. Next from Released node drag out a fiber to create “set canRotate block”. It must check off a rotatable object’s canRotate variable.
Look out, this block has a Target property because the engine should know what object’s variable to change. We made a variable which referenced to a 3d object in another class. This variable called “tank”. Let’s drag this to a Graph. Choose “get” from a context menu. Then bind “Target” with “Tank”. Now we need to know mouse coordinate position. To get this create Get Player Controller block which returns a user control object. Drag this out and create Get Input Mouse Delta. This Function returns a difference between last mouse position and current. This is what we need!
If we apply this value to an object rotation, it will rotate very slowly. Then we need to multiply this value to -10. A negative value is needed to rotate an object to an opposite direction.
Now we can build a rotator. To do this we should use a Make Rot function, find it in the context menu. Bind a multiply result with an Yaw input of this func. After that bind a result of Make Rot with input A of a Combine Rotators func. The second value of combine operations should be a current object position in a 3D space. So create Get Actor Rotation from the context menu. Note a target to this func should be a tank as ever. A return value of a Combine Rotators is a complete rotator which we can use to rotate our 3d object. So we need to make a new func “Set Relative Rotation” from the context menu. This func needs a target, you know this should be a tank, if you drag a fiber from a tank to this block, Blueprint makes a conversion automatically.
Now we should roll back little. As you see now “Set Relative Rotation” func doesn’t get a control. So we need to create a new event: Event Tick which called every tick. This event calculates a logic branch where a condition is canRotate value. If it is true then a control is passed to “Set Relative Rotation” func. And this func is rotates our object. We saw an one way of an execution of our program when an object rotated by mouse move.
The second way an object rotates by itself inertia. When a player release a mouse button a mouse speed (delta) stores in a variable. And an object rotates with this speed. Ok, when does our program store a delta (speed)? After an execution of a “Set Relative Rotation” func control is passed to logic branch and if canRotate is true, a program changes a value of a delta variable which we defined early.
If a condition after an Event tick is false, our program executes another “Set Relative Rotation” func which get another parameters. Now a delta is took up from a variable “delta”. Other operations are the same: Make Rot, Combine Rotators, Get Actor Rotation and in the capacity of the target is a “tank” variable.
Note a delta variable has an initial value. This allows an object rotates from begin of a program execution. Then an user can change a speed and a direction of a rotation.
So now we examined a rotation of an 3d object in the 3d scene completely.
If you’ve got questions, ask me.
Thank you for reading!