LookAtCameraYonly

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "</csharp>" to "</syntaxhighlight>")
Line 12: Line 12:
 
==C# - LookAtCameraYonly.cs==
 
==C# - LookAtCameraYonly.cs==
 
<syntaxhighlight lang="csharp">  
 
<syntaxhighlight lang="csharp">  
using UnityEngine;
+
using UnityEngine;
 
  using System.Collections;
 
  using System.Collections;
 
    
 
    
 
  public class LookAtCameraYonly : MonoBehaviour
 
  public class LookAtCameraYonly : MonoBehaviour
 
  {
 
  {
     public Camera cameraToLookAt;
+
     public Transform cameraTransform;
 
+
    privateTransform myTransform;
 +
 
 +
    void Awake()
 +
    {
 +
        myTransform = transform;
 +
    }
 +
 
 
     void Update()  
 
     void Update()  
 
     {
 
     {
         Vector3 v = cameraToLookAt.transform.position - transform.position;
+
         Vector3 v = cameraTransform.position - myTransform.position;
 
         v.x = v.z = 0.0f;
 
         v.x = v.z = 0.0f;
         transform.LookAt(cameraToLookAt.transform.position - v);  
+
         myTransform.LookAt(cameraToLookAt.transform.position - v);  
 
     }
 
     }
 
  }
 
  }
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 02:52, 5 July 2013

Author: Neil Carter (NCarter)

Description

This script will rotate a GameObject on its Y axis so that it is always facing the selected camera. It is useful for making camera facing billboards.

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.

C# - LookAtCameraYonly.cs

 
 using UnityEngine;
 using System.Collections;
 
 public class LookAtCameraYonly : MonoBehaviour
 {
    public Transform cameraTransform;
    privateTransform myTransform;
 
    void Awake() 
    {
        myTransform = transform;
    }
 
    void Update() 
    {
        Vector3 v = cameraTransform.position - myTransform.position;
        v.x = v.z = 0.0f;
        myTransform.LookAt(cameraToLookAt.transform.position - v); 
    }
 }
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox