Rotate Yaw and Pitch with mouse and/or touch

This was not my first port of call. I’ve spent the last few days browsing countless posts with the same general idea.

What I am trying to achieve is simple. I want to rotate an object (A cube, possibly with other objects attached but I think I can do that and group them some how… I’ll get to that later though), by clicking and dragging. The real goal is to use touch to do it but for debug purposes and the fact that its not a difficult task to change, the mouse will do. I want to be able to click, and drag quickly and when I let go, I want the object to slow down and stop rotating. The camera remains stationary.

One of the problems I seem to be having with the things I find online is that if I try to paste in the code, Unreal crashes. Also, I don’t know whether to put it in the Object blueprint (Select the object, click add blueprint) or the level blueprint and often when attempting to attach a Target node, the cube target isn’t present and I can only “Cast to cube”.

These are my main problems. How would I do this in a blueprint form?

Thanks and sorry!

I might be able to help you. But you could go in detail, what you want to do?

Do you have a character?
A floating camera with interaction?
Do you want to spin in your objects, or are they nested in your character blueprint?

Would be nice of you, if you could tell, how you want your game to work. That way I could help you :).

Well, I have a stationary floating camera, pointing down towards the cube on an angle. When I am looking through that camera (which is always), I wan’t to be able to click and drag (Preferrably with touch) and have the cube rotate around its center point. I only need rotation on 2 axis. The up down axis and the left right axis, no need to twist. (Like moving a sim’s head around or a minecraft character’s head).

When its working, I should be able to click play, and then click and drag/touch and drag the cube around it’s center point. (Rotating on the spot).

Thanks for helping !


Here is a little video, how to set up the pawn itself and the game mechanic. The cube is nested inside the player pawn. If you want to use objects outside of the player pawn, you could simply use a line trace and get the reference and then use the add local rotation on the hit object from a linetrace :).

You need to toy around with this system a bit, because I don’t know, how you want to have it working in the end. This might just be a nudge into the right direction :).

Hope it helped.

1 Like

So, that is almost perfect! Thank you so much . I added an Invert Rotator so click drag left rotates the cube to the left. One small problem, when the cube is flipped vertically, the horizontal controls are wrong and they become inverted. I thought about adding a Branch but the best way to do it would be relative. I gave addRelativeRotation a chance but to no avail. Any help is greatly appreciated! Thanks!


I will look into it eventually, when I get time for it :). I used the add local rotation, to avoid gimbal lock (3 axis occupy the same space, so flipping of the target object occurs). I think the system just needs some kind of reference point.

EDIT: Try this: You might need to invert your rotators again, but it keeps your axis direction consistent.

Create a null object like a sphere next to your cube mesh in your blueprint class actor. (cube and sphere are equal levels) Upon mouse down, PARENT the cube UNDER the Sphere… Rotate the sphere (not the cube). Upon mouse UP - UNPARENT cube… RESET rotations on Sphere back to 0,0,0.

What this does is resets the sphere everytime you let go of the mouse button. So, no matter what orientation the cube is in, it will rotate correctly (left makes it go left, etc). Hope that helps.

I might be doing this wrong, but detaching the cube makes it invisible and attaching the cube doesn’t allow me to rotate it.
Thanks for all your help guys! We’re almost there.

Also, , your trick changed absolutely nothing. It still has bad rotation.

EDIT: Oh my god I’m an idiot. I can see why its not rotating now! Still not working though :frowning:

EDIT2: Seems like a restart of unreal made 's code work. Huh, oh well. Thanks alot @!


***Thank you everyone! This is the final, working code. ***
For anyone else using this, the y axis is not locked and so making small circles with the mouse will cause unexpected rotation on that axis.

Glad that you got it working. Well… I tend to look at the Unreal Engine like a living being. It sometimes is a little bit tricky to deal with. Restarting sometimes works wonders :).

Any tips to get this also working with touch? Any help would be appreciated.