CameraRelativeScale
From Unify Community Wiki
(Difference between revisions)
(Created page with "Category: Camera Category: MonoBehaviour Category: C Sharp Author: Hayden Scott-Baron (Dock) ==Description== This script scales an object relative to a camera's di...") |
(→Description) |
||
Line 5: | Line 5: | ||
==Description== | ==Description== | ||
This script scales an object relative to a camera's distance. This gives the appearance of the object size being the same. | 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. | + | Useful for GUI objects that appear within the game scene. |
+ | Often useful when combined with [[CameraFacingBillboard]]. | ||
==Usage== | ==Usage== |
Latest revision as of 13:43, 19 October 2012
Author: Hayden Scott-Baron (Dock)
Contents |
[edit] Description
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.
[edit] Usage
Place this script on the gameobject you wish to keep a constant size.
[edit] 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.
[edit] C# - ScaleRelativeToCamera.cs
/// ScaleRelativeToCamera.cs /// Hayden Scott-Baron (Dock) - http://starfruitgames.com /// 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; } }