Simulating 3D object rotation in a 2D space/system?

Hello, the title might be confusing, let me clear it out.

I’m trying to recreate the “Examine” feature in Resident Evil Remake but using the mouse instead of the arrow keys. I have found this script, but the problem with it, well… hard to explain by words, take a look at the pictures below. I’m gonna be using the arrows keys for simplicity’s sake, here’s the code for rotation:

void Update()
{
	horizRot -= Input.GetAxis("Horizontal") * speed * friction;
	vertRot += Input.GetAxis("Vertical") * speed * friction;
	fromRotation = transform.rotation;
	toRotation = Quaternion.Euler(vertRot, horizRot, 0);
	transform.rotation = Quaternion.Lerp(fromRotation, toRotation, Time.deltaTime * lerpSpeed);
}

Now take a look at the next pair of images to understand the problem:

In the left image, pressing left/right will rotate the cube around the y-axis, to the right/left, which is exactly what’s expected from it.
Same thing for the right one, pressing up/down will rotate around the x-axis going up/down. Everything’s good, so far…

Now look at this pair:

In the left image, if you press left/right you will actually rotate around the z-axis, and not the ‘y’, which what the script should do, that way, it’s actually doing ‘what I want’ and not ‘what it should do’, why is it doing that? - shouldn’t it rotate around the y-axis?

In the right image, is the reason I asked this question, if you press up/right, you will actually rotate around the x-axis, which is ‘what the script should do’ and not ‘what I want’! (the opposite of the previous situation) I want it to rotate around the z-axis!

I hope you understand what I’m trying to achieve now, and why did I say in my title ‘simulate 3d object rotation in a 2d space/system’ - because the way I want it, is rotate as if it’s only a 2d system, no depth axis, as if we’re not rotating in world space…

how can I achieve that? how can I rotate a 3d object in a 2d space?

Thanks for any help.

You can’t really rotate a 3D object in 2D space.
But you can use a referential.

My guess is, you’re always using the worlds axes, not the displayed local object’s axes. (the transform.rotation is the world orientation of your object, with the world axes as referential)
In your first example things just happen to coincide, in both cases.

What you could do, is

1/ create a dummy.

2/ Set it’s position to that of the gameObject,

3/ and the orientation to that of your camera.

4/ Once that is done, set the dummy as a parent of your to-handle-object.

5/ And instead of using rotation, use localRotation.

That way, the referential aceswill always be Y for rotating it horizontaly, and X, for rotating it verticaly.

I guess it would be one of the “easiest” ways, otherwise you probably have to start playing around with quaterions.