How do I link Position and Scale?

Hi everyone,

I am an animator and not a programmer so please bare with me as I do my best to ask a programmy question :slight_smile:

I created a photoshop scene which has 3 layers: Foreground, Midground and Background.

I imported these layers onto 3 separate planes in Unity as textures and set the planes up in 3D space so I could create a parallax effect between the 3 layers when I move the camera.

When I moved the Midground and Foreground planes closer to the camera I had to scale them down so they maintained the correct size in relation to the Background.

Does anyone know if there a way in Unity to link the position and scale properties of a plane? I have lots of scenes to set up in Unity so it would be awesome if the planes scaled down automatically as I moved them closer to the camera.

Thanks

You probably want an Orthographic camera.

http://unity3d.com/support/documentation/Components/class-Camera.html

Here, try this script. The “scale” parameter is the global scale factor, which should be set the same for all layers. Change “targetCamera” if you want a camera other than the main camera controlling the scale.

using UnityEngine;

[ExecuteInEditMode]
class ScaleFromDistance : MonoBehaviour
{
    public Camera targetCamera = null;
    public float scale = 1;
    void Update()
    {
        Camera usedTargetCamera = (targetCamera == null) ? Camera.main : targetCamera;
        float overallScale = scale * usedTargetCamera.transform.InverseTransformPoint(transform.position).z;
        transform.localScale = Vector3.one * overallScale;
        if (Application.isPlaying)
        {
            Destroy(this);
        }
    }
}