CameraFacingBillboard

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Reverted edit of 63.146.21.70, changed back to last version by NCarter)
m (tabs -> spaces)
Line 21: Line 21:
 
public class CameraFacingBillboard : MonoBehaviour
 
public class CameraFacingBillboard : MonoBehaviour
 
{
 
{
public Camera m_Camera;
+
    public Camera m_Camera;
  
void Update()
+
    void Update()
{
+
    {
transform.LookAt(transform.position + m_Camera.transform.rotation * Vector3.back,
+
        transform.LookAt(transform.position + m_Camera.transform.rotation * Vector3.back,
m_Camera.transform.rotation * Vector3.up);
+
            m_Camera.transform.rotation * Vector3.up);
}
+
    }
 
}
 
}
 
</csharp>
 
</csharp>

Revision as of 15:56, 11 February 2007

Author: Neil Carter (NCarter)

Contents

Description

This script makes the object which it is attached to align itself with the camera. This is useful for billboards which should always face the camera and be the same way up as it is.

Usage

Place this script on a GameObject that you want to face the camera. Then, with the object selected, use the inspector to select the Camera you want the object to face.

You might want to change Vector3.back to Vector3.front, depending on the initial orientation of your object.

Technical Discussion

Note that the script doesn't simply point the object at the camera. Instead, it makes the object point in the same direction as the camera's forward axis (that is, the direction the camera is looking in). This might seem intuitively wrong, but it's actually correct for the one-point-perspective world of realtime computer graphics.

C# - CameraFacingBillboard.cs

<csharp>using UnityEngine; using System.Collections;

public class CameraFacingBillboard : MonoBehaviour {

   public Camera m_Camera;
   void Update()
   {
       transform.LookAt(transform.position + m_Camera.transform.rotation * Vector3.back,
           m_Camera.transform.rotation * Vector3.up);
   }

} </csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox