Search Unity

Reverse rotation problem

Discussion in 'Scripting' started by stephencoorey, Feb 15, 2018.

  1. stephencoorey

    stephencoorey

    Joined:
    Mar 29, 2017
    Posts:
    10
    I am trying to solve an AR problem that can be simplified into a basic maths example:

    Lets say i have a cube at 0,0,0 and I point my phone camera at it from a position of say (-20, -15, -10) and the rotation of the phone camera is something like (5, 10, 20) to make it look at the cube from an angle.

    i press a button to go to another scene, and i want to set up the scene such that the camera is now positioned at 0,0,0 and it points to a cube that is the same distance and angle from the camera as the original cube was in the first scene.

    So in the first scene i store the opposite position of where i think the 2nd cube should go (multiply x, y, z by -1 to give (20, 15, 10)). and this seems to work on its own, when i use that to place the cube in the 2nd scene it is in the right spot.

    Now the hard part: I store the rotation of what the 2nd cube should be (the opposite of what the camera is rotated in the first scene, like what I did with position) like this:

    Code (CSharp):
    1.         var cameraTransformRotation = new Vector3(Camera.main.transform.rotation.eulerAngles.x * -1, Camera.main.transform.rotation.eulerAngles.y * -1, Camera.main.transform.rotation.eulerAngles.z * -1);
    2.  
    and try to apply it to the cube in the 2nd scene like this:
    Cube.transform.Rotate(-5, -10, -20);

    and it puts the cube in a totally different spot, whether I rotate it before or after I translate it.

    I have tried storing Camera.main.transform.InverseTransformPoint(cube.transform.position) in the first scene and applying that to the cube in the 2nd scene to do the position and rotation at the same time, but this was also way off the expected position of the cube. The idea here is to get the local position of the cube relative to the camera, but I think my maths understanding is incorrect.

    Any help would be appreciated to help me understand how to apply the rotation while moving the cube.
     
  2. faxedhead

    faxedhead

    Joined:
    Dec 21, 2014
    Posts:
    3
    Before switching scenes, save the camera position and rotation as follows:

    var cameraPosition = Camera.main.transform.position * -1;
    var cameraRotation =Camera.main.transform.rotation.eulerAngles * -1;

    Then in the next scene, make sure to apply the rotation before the translation as follows:

    OriginCube.transform.Rotate(TransformHolder.CameraRotation);
    OriginCube.transform.Translate(TransformHolder.CameraPosition);

    Attached is an example Unity project showing this in action, use WASD to move the camera, and RFTG to rotate.
     

    Attached Files:

    stephencoorey likes this.