How can I rotate along X Z while my Y is not affected?

Hello,

I am trying to rotate a gameobject (say Cube of 4x1x4). My game is Top-Down view and the Gameobject falls from
the TOP and is acted upon gravity. While I am trying to control the movement of the gameobject, I am trying it to rotate in the fashion I tilt my handheld device. Say I place my handheld - horizontal to the ground and If I happen to tilt it towards myself I would like to see the gameobject tilt in the same way as my handheld. If I could say, The gameobject should act like a mirror to the ipad.

However though, during the game the gameobject suffers rotation along Y axis. If it so happens, with the code below the object rotates to it’s X axis rather than rotating the object in the way I tilt it. Also I would like to note that my tilt at most not affect the transform rotations along Y axis.

CODE :

targetRotation = Quaternion.Euler(reqAngle.x, transform.eulerAngles.y, reqAngle.z); // Y is from it's transform
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime * 100f);

INPUT :
My input is based of acceleration and I calculate the angles from those values.

Developers, I am working alone and I am stuck here. I am not sure why and what is the problem or how to tackle it in
another approach. Your help is much appreciated.

Thank you Please…

IMAGES :

With Y Rotation

Without Y rotation

What Happens

What I require

why dont you try this:

Quaternion targetRotation;
float reqAngleX;
float reqAngleZ;
reqAngleX = //here goes the value of the angle you want to rotate to in x;
reqAngleZ = //here goes the value of the angle you want to rotate to in z;
targetRotation = Quaternion.Euler(reqAngleX, transform.rotation.y, reqAngleZ);
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime);

I would recomend not to multiply the deltaTime till you get it working as you wont, just for debugging reasons.

Matiasgil as it is Quaternion changing or accessing individual variable (say ‘y’ or ‘w’) is not recommended by unity itself. Thats why I went for Euler angle.
Eventhough I did try it out. It did create some noise.

Did you try and did it work for you? If so let me know, I might have made a mistake somewhere.

no i didnt test it, i will test it tomorrow and i will give you a feedback.

Thanks Matiasgil. I appreciate your help. I wish to use angles rather than Vectors as angle help to solve my issue.

If you want to use angles, rather than vectors you can use this:

Quaternion angle;
angle = Quaternion.AngleAxis(10, transform.up); /* transform.up is the normalized vector the angle is calculated with */
transform.rotation = Quaternion.RotateTowards(transform.rotation, angle, Time.deltaTime);