- Home /
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 :)
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
Answer by flaviusxvii · Jun 23, 2011 at 04:33 PM
You probably want an Orthographic camera.
http://unity3d.com/support/documentation/Components/class-Camera.html
Thanks for the speedy response. Unfortunately with an Orthographic camera there won't be any sense of 3D space/Depth to the scene i.e. the Foreground, $$anonymous$$idground and Background planes will all move together at the same speed when I move the camera and there won't be any parallaxing effect.
Ah.. I misunderstood you. In that case I'd use this http://unity3d.com/support/documentation/ScriptReference/GeometryUtility.CalculateFrustumPlanes.html and then interpolate between the height of the close plane and the far plane to get the right height for any give layer at any given distance.
but you could move the foreground, middle and background planes at different speeds to recreate the parallex effect.
Answer by sneftel · Jun 23, 2011 at 05:05 PM
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);
}
}
}
Your answer