Rotate an object in all directions using the mouse

Hi all,

I want to create a mini-game which start with a 3D object at the center of the screen. A user needs to rotate an object in all directions by using the mouse. Same as you would look at a 3D object in a web browser e.g. like this one.
After that, the user needs to be able to click on certain areas of the object (which I guess would be separate assets that are attached to the same object e.g. as components in a blueprint).

I can’t use the default templates since they always start from a user pawn (first person, third person etc).

What would be the best way to do this in Unreal Engine?

Thanks a lot for your advice.

Rotating an object is rather straighforward:

Image from Gyazo

After that, the user needs to be able
to click on certain areas of the
object

Child Actors Components sound like the most obvious solution if the attached elements need to define their own unique logic. Depending on what is required (complexity of the shape of the object, modularity, scope) you could have sockets on a static mesh and add Child Actors Components procedurally.

I can’t use the default templates
since they always start from a user
pawn (first person, third person etc).

I do not know what this has to do with anything.

Hi Everynone, thanks a lot for your answer. Just to be clear what I did: I created a blueprint of the object I want to rotate (aMouseRotate). From the level blueprint I cast to that object which is also in my scene. In the level blueprint, I made a variable (RotateThis) which is an object reference to that blueprint. Is that what I should do? You screenshot is a little fuzzy about that. When I play my level, the object is indeed rotating (kinda, it’s not really rotating around it’s z-axis but rather leaning to the left or right) but I’m also dragging my entire scene along (camera also moves along with the mouse cursor). How I can stop this?

Hi Everynone, thanks for your clarification. I’ve updated my code (everything is inside one blueprint). I think that part should be fine. Thanks for that. The only issue that remains is that it doesn’t rotate properly. Here’s a .gif attached.

299407-rotatingcube-small.gif

Looks like all you need to do is to reverse the +/- on the multiplier. Or use another axis.

In the level blueprint, I made a
variable (RotateThis) which is an
object reference to that blueprint. Is
that what I should do? You screenshot
is a little fuzzy about that.

Apologies. Here’s a tad more thought behind the reasoning.

The code I posted will work in any blueprint. If you do use it outside of the Player Controller, you will need to Get Player Controller for the 3 nodes that need it as a Target; the 3rd one being the mouse cursor visibility.

The script selects a clicked object so you can use it with any object anywhere. If you already have a reference (as mentioned below), you can skip the left side completely and just use this:

You can even skip validation if you’re certain the reference is always valid. I’m assuming you’re attempting it all in the Level Blueprint. Generally speaking I’d avoid doing anything in the Level Blueprint that is not unique to that level. For example: if you load a different level for a different scene, you’ll need to rewrite the script. Again, it depends on how modular and grand in scope the project is supposed to be. If you never load another level, it’s OK to script in the LB. It still makes the communication a bit awkward at times, though.


I’m also dragging my entire scene
along (camera also moves along with
the mouse cursor

It’s the default Pawn’s behaviour. Create your own Pawn, create a custom Game Mode and assign the Pawn to it. Finally, start using the Game Mode. This will give you a Pawn that does not have any functionality built in. More info:

You will need this anyway in order to control the flow of data. In addition, the framework classes mentioned above expose a lot of functionality and often serve as glue between various elements of your project.

From the level blueprint I cast to
that object which is also in my scene.
In the level blueprint, I made a
variable (RotateThis) which is an
object reference to that blueprint.

There’s no need to cast. If you have something in the Level Blueprint, you can reference it directly by selecting an actor in the scene and right clicking in the LB graph and choosing Create a Reference To… Drag and drop also works in this case. But it does sound like you’ve done it already since you see things lean.

it’s not really rotating around it’s
z-axis but rather leaning to the left
or right

Perhaps you can share a screenshot. We can try to troubleshoot.

Solved it by connecting DElta X (value -10) to DElta Rotation Z and Delta Y (value 10) to Delta Rotation Y. Also had to add “Set Input Mode Game Only” to level blueprint in order to make it work.
For those interested: I also found another solution where you rotate around the object, instead of rotating the object itself. Both can serve specific purposes: UE4 Camera Rotation Bueprint - YouTube

ummm… I think it’s rotating around the cube’s pivot (on the bottom)… just put the pivot in the center of the box and it should be fine…

I did not even notice that but you’re right, of course. Although the direction still looks reversed. Might be by design, though.

Thanks for your suggestions! I solved it by connecting the right nodes.

I also had to add “Set Input Mode Game Only” in the level blueprint to be able to interact with the object.

hey guys, how to do this exact same thing with touch input? the object have to rotate based on the touch movement on the screen(swiping). Could somebody give a solution?