1. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  2. Unity 2017.2 beta is now available for download.
    Dismiss Notice
  3. Unity 2017.1 is now released.
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  6. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice

Rotating object according to camera rotation

Discussion in 'Virtual Reality' started by nikhoney27, Jul 17, 2017 at 10:18 AM.

  1. nikhoney27

    nikhoney27

    Joined:
    May 31, 2017
    Posts:
    1
    I am using rajawali to render a 360 image , where i am putting one object in front of camera which is 2d object that should always remain at the center of screen. Therefore the object needs to be rotated with respect to camera rotation.

    It works fine when camera rotates around x axis and y axis but it does not work properly when rotated around z axis.

    So any one change help me out how i can achieve this.

    So currently i am using below code to put object in center and rotation of plane.

    public void centerObject(Plane obj) {
    Vector3 position;
    float[] newPosition4 = new float[4];
    position = obj.getPosition();
    float[] posVec4 = {0, 0, -16, 1.0f};
    float[] HeadViewMatrix_inv = new float[16];
    Matrix4 HeadViewMatrix4 = new Matrix4();
    HeadViewMatrix4.setAll(sphere.getModelViewMatrix());
    HeadViewMatrix4 = HeadViewMatrix4.inverse();

    //Copy matrix to HeadViewMatrix array,
    HeadViewMatrix4.toFloatArray(HeadViewMatrix_inv);

    //Translation of viewMatrix
    Matrix.multiplyMV(newPosition4, 0, HeadViewMatrix_inv, 0, posVec4, 0);

    obj.setPosition(newPosition4[0], newPosition4[1],
    newPosition4[2]);

    double localCameraRx = ((getCurrentCamera().getRotX() * 57.2958) + 180) / 57.2958;
    double localCameraRy = ((getCurrentCamera().getRotY() * 57.2958) + 180) / 57.2958;
    double localCameraRz = ((getCurrentCamera().getRotZ() * 57.2958) + 180) / 57.2958;


    if (cameraRX != localCameraRx) {
    obj.setRotation(Vector3.X, (cameraRX - localCameraRx));
    cameraRX = localCameraRx;
    }
    if (cameraRY != localCameraRy) {
    obj.setRotation(Vector3.Y, (cameraRY - localCameraRy));
    cameraRY = localCameraRy;
    }
    if (cameraRZ != localCameraRz) {
    obj.setRotation(Vector3.Z, (cameraRZ - localCameraRz));
    cameraRZ = localCameraRz;
    }

    }
     
  2. Selzier

    Selzier

    Joined:
    Sep 23, 2014
    Posts:
    447
    Can't you just make the object a child of the camera?
     
    SiliconDroid likes this.
  3. SiliconDroid

    SiliconDroid

    Joined:
    Feb 20, 2017
    Posts:
    146
    Hi,

    Simplest way is to just make the object a child of the camera and then add some localspace Z to set its depth:

    Code (CSharp):
    1.         ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    2.         //    BIND OBJ TO CAM WITH Z PUSH
    3.         bool BindObjToCam(GameObject oObj, float fPushZ)
    4.         {
    5.             //  PREPARE
    6.             if(!oObj)
    7.             {
    8.                 return false;
    9.             }
    10.             Transform tObj = oObj.transform;
    11.  
    12.             Camera oCam = Camera.main;
    13.             if (!oCam)
    14.             {
    15.                 return false;
    16.             }
    17.  
    18.             Transform tCam = oCam.transform;
    19.             if (!tCam)
    20.             {
    21.                 return false;
    22.             }
    23.          
    24.             //  BIND
    25.             tObj.parent = tCam;
    26.             tObj.SetPositionAndRotation(tCam.position, tCam.rotation);
    27.             tObj.Translate(0, 0, fPushZ, Space.Self);
    28.             return true;
    29.         }
    30.  
    EDIT: as @Selzier says!:)
     
    Selzier likes this.