How to rotate an object according to the camera's view?

I HAVE ATTACHED A DIAGRAM DISPLAYING WHAT I MEAN

Basically, my goal is this: I want a script I can attach to an object that rotates it according to the camera’s view. As in, if a cube with the script attached is in front of the camera, and I press W, it rotates upward so that according to the camera, the cube face that was facing the camera is now facing north, the cube face that was facing south is now facing the camera, etc. I’ve been able to almost do this using quaternion.angleAxis and then lerping, but it rotates the object according to its own orientation, not the cameras, and that leaves the rotation feeling random as it does not act like I described above. I would very much appreciate any help! Thanks.

Here Simple code for rotate your object considering camera view

if the camera transform is rotated, this makes your cube rotate correct direction for view

    if (Input.GetKeyDown("a"))
    {
        cubeTransform.Rotate(cameraTransform.up, 90, Space.World);
    }
    if (Input.GetKeyDown("d"))
    {
        cubeTransform.Rotate(cameraTransform.up, -90, Space.World);
    }
    if (Input.GetKeyDown("w"))
    {
        cubeTransform.Rotate(cameraTransform.right, 90, Space.World);
    }
    if (Input.GetKeyDown("s"))
    {
        cubeTransform.Rotate(cameraTransform.right, -90, Space.World);
    }
  • “cubeTransform” for your object to rotate,
  • “cameraTransform” for your camera object’s transform

code tested on 2017.3.1p4