Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Object following the camera with current coordinates as middle point to rotate around.

Discussion in 'AR/VR (XR) Discussion' started by s096268, Nov 4, 2015.

  1. s096268

    s096268

    Joined:
    Oct 28, 2015
    Posts:
    11
    Hi guys,

    I'm using Vuforia tags to (virtually) load a cube that follows the camera using the following (javascript) script:

    Code (csharp):
    1.  var target : Camera;
    2.  
    3.  
    4. function Update() {
    5.    
    6.     var n = target.transform.position - transform.position;
    7.     transform.rotation = Quaternion.LookRotation(n) * Quaternion.Euler(0, 0, 0);
    8.     transform.eulerAngles.z = 0;
    9.  
    10. }
    this works perfectly well if the middle point of the tag and the cube are alligned. However I want the cube to rotate around its own center while the middle points of the tag and cube are NOT alligned.

    Using this script, with unalligned middle points, the cube will turn around the middlepoint of the tag because the cube is a child of the tag.

    Thus, my question: how can I make the cube follow the camera, while the centers of the tag and cube are not alligned and the cube is a child of the tag?

    Hope everything is clear.

    Thanks
     
  2. s096268

    s096268

    Joined:
    Oct 28, 2015
    Posts:
    11
    Nevermind, I figured it out!

    For people with a similar problem:

    I made an empty game object as child of the marker. I displaced this empty gameobject to the desired position using the coordinates in the inspector. Then I made the actual cube a child of that empty game object to ensure that the coordinates of the cube are 0,0,0 and therefore the script makes it turn around it own mid point.

    Feel free to ask if this is not clear.