unity3d - Vuforia: Move object without scaling


Keywords:unity3d 


Question: 

I am working on a project in Vuforia with Unity and I set a VuMark in order to track an object with the ARCamera.

I would like to lock the size an object (which is set on the VuMark) in the virtual space so that when I get the VuMark near to the camera, the size of the object in the virtual should stay the same.

I tried to lock the Y-Axis in Unity Inspector, however it does not solve the issue.

Does anybody have an idea on how to keep the size of the object the same regardless of a specific axis in the virtual space or if the object gets nearer to the camera?


1 Answer: 

Obviously, your camera can move on the X, Y, and Z axis, so freezing the Y axis won't have any bearing on how close the camera can get to the vumark.

On top of that, with the way Vuforia works, the plugin will either move the camera in world space or it'll move the VuMark object. And which one your project is set up to use wasn't included in your question (or which of the two you tried locking the axis for).

The way to go about solving this is to actually calculate the distance from the camera (regardless of which object is moving, this calculation will be accurate) and then supply that value back to the 3D object's scale (with some multiplier).

Here's the script I used to test:

public class KeepSize : MonoBehaviour {
    void Update () {
        float dist = (Camera.main.transform.position - transform.position).magnitude / 3f;

        transform.localScale = new Vector3(dist, dist, dist);
    }
}

The division by 3 is an arbitrary value that made a simple cube fit on screen nicely.