0
votes

I have an 3D model attached to a image target. What I want is to scale the model according to the camera's position, i.e. when I get closer to the model I want it to get larger and when I get far away it should get smaller. I searched the forums and tried two different solutions: Solution 1: I created a script and attached to the 3d model:

public class AR_Capsule : MonoBehaviour {

    float minimumDistance = 0;
    float maximumDistance = 3;
    float minimumDistanceScale = 0.3f;
    float maximumDistanceScale = 0.1f;  

    void Update()
    {
        float distance = (transform.position - Camera.main.transform.position).magnitude;

        float norm = (distance - minimumDistance) / (maximumDistance - minimumDistance);

        norm = Mathf.Clamp01(norm);

        Vector3 minScale = Vector3.one * maximumDistanceScale;
        Vector3 maxScale = Vector3.one * minimumDistanceScale;

        transform.localScale = Vector3.Lerp(maxScale, minScale, norm);
    }
}

Solution 2:

public class AR_Capsule : MonoBehaviour {

    public float speed = 10f;

    void Update()
    {
        transform.Translate(0, 0,(-1 * Input.acceleration.z * speed * Time.deltaTime));
    }
}

Both solutions work when the image target is visible. But when I lose the target from sight, then the object stays still and camera position is always (0.0, 0.0, 0.0). Do you have any idea why the object freezes when the target is lost? I am developing for Epson Moverio bt-300 and working with vuforia eyewear sample app.

Thanks.

1

1 Answers

0
votes

I found the issue. The problem is about getting the correct camera position. Since I am using a eyewear Camera.main.transform.position will give always (0,0,0). Instead I used Camera.main.transform.GetChild(1).transform.position which work fine. Below is the working code:

initialDist = Vector3.Distance(transform.position, Camera.main.transform.GetChild(1).transform.position);

distance = Vector3.Distance(lastMarkerpos, Camera.main.transform.GetChild(1).transform.position);

if((transform.localScale.x * (initialDist / distance)) > 0.05 && (transform.localScale.x * (initialDist / distance)) < 2)
{
    transform.localScale = new Vector3(transform.localScale.x * (initialDist / distance), transform.localScale.y * (initialDist / distance), transform.localScale.z * (initialDist / distance));
}