From Unify Community Wiki
Revision as of 13:43, 19 October 2012 by Dock (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Author: Hayden Scott-Baron (Dock)



This script scales an object relative to a camera's distance. This gives the appearance of the object size being the same. Useful for GUI objects that appear within the game scene. Often useful when combined with CameraFacingBillboard.


Place this script on the gameobject you wish to keep a constant size.

Technical Discussion

This measures the distance from the Camera plane, rather than the camera itself, and uses the initial scale as a basis. Use the public objectScale variable to adjust the object size.

C# - ScaleRelativeToCamera.cs

/// ScaleRelativeToCamera.cs
/// Hayden Scott-Baron (Dock) -
/// 19 Oct 2012
/// Scales object relative to camera. 
/// Useful for GUI and items that appear in the world space. 
using UnityEngine;
using System.Collections;
public class ScaleRelativeToCamera : MonoBehaviour 
	public Camera cam; 
	public float objectScale = 1.0f; 
	private Vector3 initialScale; 
	// set the initial scale, and setup reference camera
	void Start ()
		// record initial scale, use this as a basis
		initialScale = transform.localScale; 
		// if no specific camera, grab the default camera
		if (cam == null)
			cam = Camera.main; 
	// scale object relative to distance from camera plane
	void Update () 
		Plane plane = new Plane(cam.transform.forward, cam.transform.position); 
		float dist = plane.GetDistanceToPoint(transform.position); 
		transform.localScale = initialScale * dist * objectScale; 
Personal tools