LookAtCameraYonly

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
Line 17: Line 17:
 
  public class LookAtCameraYRotationOnly : MonoBehaviour
 
  public class LookAtCameraYRotationOnly : MonoBehaviour
 
  {
 
  {
public Camera cameraToLookAt;
+
    public Camera cameraToLookAt;
 
    
 
    
void Update()  
+
    void Update()  
{
+
    {
Vector3 v = cameraToLookAt.transform.position - transform.position;
+
        Vector3 v = cameraToLookAt.transform.position - transform.position;
v.x = v.z = 0.0f;
+
        v.x = v.z = 0.0f;
transform.LookAt(cameraToLookAt.transform.position - v);  
+
        transform.LookAt(cameraToLookAt.transform.position - v);  
}
+
    }
 
  }
 
  }
 
</csharp>
 
</csharp>

Revision as of 09:32, 2 December 2005

Author: Neil Carter (NCarter)

Description

This script will rotate a GameObject on its Y axes so that it is always facing the selected camera. It is useful to make camera facing billboards.

Usage

Place this script on a GameObject that you want to face the camera. Then, when the object is selected, you need to select the Camera you want the object to look at in the inspector.

C# - LookAtCameraYonly.cs

<csharp> using UnityEngine;

using System.Collections;
 
public class LookAtCameraYRotationOnly : MonoBehaviour
{
   public Camera cameraToLookAt;
 
   void Update() 
   {
       Vector3 v = cameraToLookAt.transform.position - transform.position;
       v.x = v.z = 0.0f;
       transform.LookAt(cameraToLookAt.transform.position - v); 
   }
}

</csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox